(三)PostgreSQL的pg_ctl命令

PostgreSQL的pg_ctl命令

pg_ctl 是 PostgreSQL 用于控制数据库服务器进程的命令行工具。它提供了启动、停止、重启数据库服务器以及管理其运行状态的手段。pg_ctl 命令尤其适用于从命令行或脚本中管理 PostgreSQL 服务,而不是通过操作系统的服务控制管理器。

sql 复制代码
基础信息
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. 启动数据库服务器

    bash 复制代码
    pg_ctl start -D /home/pg16/data
sql 复制代码
[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. 停止数据库服务器

    bash 复制代码
    pg_ctl stop -D /home/pg16/data
sql 复制代码
 [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. 重启数据库服务器

    bash 复制代码
    pg_ctl restart -D /home/pg16/data
sql 复制代码
[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. 查询数据库服务器状态

    bash 复制代码
    pg_ctl status -D /home/pg16/data
sql 复制代码
[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查看

sql 复制代码
[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 管理中非常强大的工具,只需通过简单的命令行操作就可以有效地控制数据库服务器。

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

相关推荐
Lonely 净土3 小时前
Linux 运维文件写入操作
linux·运维·服务器·文件管理
城管不管3 小时前
重生——第十一次面试之挖财一面2026.8.19已OC
java·服务器·jvm·数据库·spring·面试·职场和发展
倔强的石头1063 小时前
向量数据库从相似度检索走向融合数据底座
数据库
飞英思特无线充电3 小时前
严苛场景巡检机器人的无线补能设计:点位、BMS、认证与运维边界
运维·机器人
Jimmy_jimi4 小时前
利用nginx代理完成url重定位
运维·nginx
今天AI了吗4 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
liuyicenysabel4 小时前
多服务上线日记三:
运维·服务器·笔记·学习
lilian2335 小时前
Harmony os 技术实战|拼豆制图27:用单字符编码承载 50 张 70×70 图纸
前端·数据库·华为·harmonyos
一只鹿鹿鹿5 小时前
大数据中心安全系统解决方案(Word文件)
大数据·运维·物联网·安全·政务
皮卡丘不断更5 小时前
手机优先的 Personal Ledger:把账目、学习和复盘放到同一个入口
数据库·sqlite·fastapi·开源项目·个人效率