0
点赞
收藏
分享

微信扫一扫

(三)PostgreSQL的pg_ctl命令

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 命令

  1. 启动数据库服务器

    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` 参数来指定日志文件的位置。
  1. 停止数据库服务器

    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`(不等待,直接关闭数据库,可能需要恢复操作)。
  1. 重启数据库服务器

    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` 参数指定停机模式。
  1. 查询数据库服务器状态

    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 <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

注意事项

  • 使用 pg_ctl 命令时,确保你有足够的权限。在许多系统中,可能需要以 PostgreSQL 用户(通常名为 postgres)的身份运行这些命令。
  • 请小心使用停机模式,特别是 immediate 模式,因为它可能导致数据未被完全写入磁盘而损坏。
  • 如果使用的是某些 Linux 发行版本或 Windows,PostgreSQL 服务可能通过操作系统的服务控制管理器来管理。在这种情况下,也可以使用系统命令(如 systemctlservice 命令,Windows 服务管理器)来管理 PostgreSQL 服务。

总结

pg_ctl 是 PostgreSQL 管理中非常强大的工具,只需通过简单的命令行操作就可以有效地控制数据库服务器。

谨记:心存敬畏,行有所止。

举报

相关推荐

0 条评论