(三)PostgreSQL的pg
PostgreSQL的pg_ctl命令
pg_ctl 是 PostgreSQL 用于控制数据库服务器进程的命令行工具。它提供了启动、停止、重启数据库服务器以及管理其运行状态的手段。pg_ctl 命令尤其适用于从命令行或脚本中管理 PostgreSQL 服务,而不是通过操作系统的服务控制管理器。
()
基础信息 OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo) DB版本:16.2 pg软件目录:/home/pg16/soft pg数据目录:/home/pg16/data 端口:5777
常用的 pg_ctl 命令
-
启动数据库服务器
pg_ctl start -D /home/pg16/data
[pg16@test ~]$ pg_ctl start -D /home/pg16/data waiting for server to start....2024-04-09 23:16:14.784 PDT [19476] LOG: starting PostgreSQL 16.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit 2024-04-09 23:16:14.784 PDT [19476] LOG: listening on IPv4 address "0.0.0.0", port 5777 2024-04-09 23:16:14.785 PDT [19476] LOG: listening on IPv6 address "::", port 5777 2024-04-09 23:16:14.786 PDT [19476] LOG: listening on Unix socket "/tmp/.s.PGSQL.5777" 2024-04-09 23:16:14.790 PDT [19479] LOG: database system was shut down at 2024-04-09 23:16:12 PDT 2024-04-09 23:16:14.794 PDT [19476] LOG: database system is ready to accept connections done server started
`-D` 参数指定数据库数据目录的位置,是必需的。可以添加 `-l logfile` 参数来指定日志文件的位置。
-
停止数据库服务器
()pg_ctl stop -D /home/pg16/data
[pg16@test ~]$ pg_ctl stop -D /home/pg16/data waiting for server to shut down....2024-04-09 23:16:50.021 PDT [19476] LOG: received fast shutdown request 2024-04-09 23:16:50.022 PDT [19476] LOG: aborting any active transactions 2024-04-09 23:16:50.023 PDT [19476] LOG: background worker "logical replication launcher" (PID 19482) exited with exit code 1 2024-04-09 23:16:50.024 PDT [19477] LOG: shutting down 2024-04-09 23:16:50.025 PDT [19477] LOG: checkpoint starting: shutdown immediate 2024-04-09 23:16:50.028 PDT [19477] LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.001 s, total=0.005 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB; lsn=0/17B9918, redo lsn=0/17B9918 2024-04-09 23:16:50.032 PDT [19476] LOG: database system is shut down done server stopped
停止数据库时,你可以通过添加 `-m` 参数来指定停机模式:`smart`(优雅地等待数据库关闭),`fast`(立即断开客户端连接并关闭数据库),`immediate`(不等待,直接关闭数据库,可能需要恢复操作)。
-
重启数据库服务器
pg_ctl restart -D /home/pg16/data
[pg16@test ~]$ pg_ctl restart -D /home/pg16/data waiting for server to shut down....2024-04-09 23:17:27.711 PDT [19552] LOG: received fast shutdown request 2024-04-09 23:17:27.712 PDT [19552] LOG: aborting any active transactions 2024-04-09 23:17:27.713 PDT [19552] LOG: background worker "logical replication launcher" (PID 19558) exited with exit code 1 2024-04-09 23:17:27.713 PDT [19553] LOG: shutting down 2024-04-09 23:17:27.713 PDT [19553] LOG: checkpoint starting: shutdown immediate 2024-04-09 23:17:27.722 PDT [19553] LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.001 s, total=0.010 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB; lsn=0/17B9990, redo lsn=0/17B9990 2024-04-09 23:17:27.725 PDT [19552] LOG: database system is shut down done server stopped waiting for server to start....2024-04-09 23:17:27.825 PDT [19561] LOG: starting PostgreSQL 16.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit 2024-04-09 23:17:27.825 PDT [19561] LOG: listening on IPv4 address "0.0.0.0", port 5777 2024-04-09 23:17:27.825 PDT [19561] LOG: listening on IPv6 address "::", port 5777 2024-04-09 23:17:27.827 PDT [19561] LOG: listening on Unix socket "/tmp/.s.PGSQL.5777" 2024-04-09 23:17:27.830 PDT [19564] LOG: database system was shut down at 2024-04-09 23:17:27 PDT 2024-04-09 23:17:27.833 PDT [19561] LOG: database system is ready to accept connections done server started
`restart` 命令会停止然后重新启动数据库服务器。也可以使用 `-m` 参数指定停机模式。
-
查询数据库服务器状态
pg_ctl status -D /home/pg16/data
[pg16@test ~]$ pg_ctl status -D /home/pg16/data pg_ctl: server is running (PID: 19561) /home/pg16/soft/bin/postgres "-D" "/home/pg16/data"
这会显示数据库服务器是否正在运行以及其 PID。
其他参数可以通过help查看
[pg16@test ~]$ pg_ctl --help pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server. Usage: pg_ctl init[db] [-D DATADIR] [-s] [-o OPTIONS] pg_ctl start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s] [-o OPTIONS] [-p PATH] [-c] pg_ctl stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s] pg_ctl restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s] [-o OPTIONS] [-c] pg_ctl reload [-D DATADIR] [-s] pg_ctl status [-D DATADIR] pg_ctl promote [-D DATADIR] [-W] [-t SECS] [-s] pg_ctl logrotate [-D DATADIR] [-s] pg_ctl kill SIGNALNAME PID Common options: -D, --pgdata=DATADIR location of the database storage area -s, --silent only print errors, no informational messages -t, --timeout=SECS seconds to wait when using -w option -V, --version output version information, then exit -w, --wait wait until operation completes (default) -W, --no-wait do not wait until operation completes -?, --help show this help, then exit If the -D option is omitted, the environment variable PGDATA is used. Options for start or restart: -c, --core-files allow postgres to produce core files -l, --log=FILENAME write (or append) server log to FILENAME -o, --options=OPTIONS command line options to pass to postgres (PostgreSQL server executable) or initdb -p PATH-TO-POSTGRES normally not necessary Options for stop or restart: -m, --mode=MODE MODE can be "smart", "fast", or "immediate" Shutdown modes are: smart quit after all clients have disconnected fast quit directly, with proper shutdown (default) immediate quit without complete shutdown; will lead to recovery on restart Allowed signal names for kill: ABRT HUP INT KILL QUIT TERM USR1 USR2 Report bugs to . PostgreSQL home page:
注意事项
- 使用 pg_ctl 命令时,确保你有足够的权限。在许多系统中,可能需要以 PostgreSQL 用户(通常名为 postgres)的身份运行这些命令。
- 请小心使用停机模式,特别是 immediate 模式,因为它可能导致数据未被完全写入磁盘而损坏。
- 如果使用的是某些 Linux 发行版本或 Windows,PostgreSQL 服务可能通过操作系统的服务控制管理器来管理。在这种情况下,也可以使用系统命令(如 systemctl 或 service 命令,Windows 服务管理器)来管理 PostgreSQL 服务。
总结
pg_ctl 是 PostgreSQL 管理中非常强大的工具,只需通过简单的命令行操作就可以有效地控制数据库服务器。
谨记:心存敬畏,行有所止。
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理!
部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!
图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!