(三)PostgreSQL的pg_ctl命令

news/2025/4/1 5:21:41/

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 connectionsdone
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 downdone
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 downdone
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 connectionsdone
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 PIDCommon 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 necessaryOptions for stop or restart:-m, --mode=MODE        MODE can be "smart", "fast", or "immediate"Shutdown modes are:smart       quit after all clients have disconnectedfast        quit directly, with proper shutdown (default)immediate   quit without complete shutdown; will lead to recovery on restartAllowed signal names for kill:ABRT HUP INT KILL QUIT TERM USR1 USR2Report 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 管理中非常强大的工具,只需通过简单的命令行操作就可以有效地控制数据库服务器。

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


http://www.ppmy.cn/news/1417826.html

相关文章

实时智能应答3D数字人搭建3

看效果 实时智能应答3D数字人2 语音输入&#xff0c;实时对话&#xff0c;3D模型&#xff0c;人物和声音、话术都可以定制。 参考&#xff1a; 实时智能应答3D数字人搭建2实时智能应答3D数字人搭建2 - 神经网络 - 金双石科技 - (出处: 金双石科技) 实时智能应答3D数字人搭建实时…

单链表经典算法题分析

目录 一、链表的中间节点 1.1 题目 1.2 题解 1.3 收获 二、移除链表元素 2.1 题目 2.2 题解 2.3 收获 2.4递归详解 三、反转链表 3.1 题目 3.2 题解 3.3 解释 四、合并两个有序列表 4.1 题目 4.2 题解 4.3 递归详解 声明&#xff1a;本文所有题目均摘自leetco…

架构设计参考项目系列主题:新零售SaaS架构:客户管理系统架构设计

什么是客户管理系统? 客户管理系统,也称为CRM(Customer Relationship Management),主要目标是建立、发展和维护好客户关系。 CRM系统围绕客户全生命周期的管理,吸引和留存客户,实现缩短销售周期、降低销售成本、增加销售收入的目的,从而提高企业的盈利能力和竞争力。 …

【数据结构】树与森林(树的存储结构、森林与二叉树的转化、树与森林的遍历)

目录 树和森林树的存储结构一、树的双亲表示法&#xff1a;二、树的孩子表示法方法一&#xff1a;定长结点的多重链表方法二&#xff1a;不定长结点的多重链表方法三&#xff1a;孩子单链表表示法 三、树的二叉链表(孩子-兄弟)存储表示法 森林与二叉树的转换树和森林的遍历先根…

Mojo语言初次入手

Mojo的出现被认为是编程语言领域的一大进步&#xff0c;有观点认为它可能是几十年来最大的编程进步???!!! 让我来试试&#xff0c;看看是怎么回事。 安装Mojo 打开命令行界面。执行以下命令来安装Mojo SDK&#xff1a; curl -s https://get.modular.com | sh - 3.接下来&…

使用formio和react实现在线表单设计

formiojs 是一个开源的在线表单设计工具&#xff0c;今天看看怎样在 react js 中使用 formiojs。 首先创建一个react工程 npx create-react-app my-react-formio-app安装依赖 cd my-react-formio-app npm install formio/react npm install formio/js另外&#xff0c;考虑样…

关于ARM的一些问题

一&#xff0c;arm的工作模式有哪些&#xff1f; User&#xff1a;非特权模式 FIQ&#xff1a;高优先级中断进入 IRQ&#xff1a;低优先级中断进入 Supervisor:当复位或软中断指令进入 Abort: 当存取异常时 Undef:当执行未定义指令时会进入这种模式 System:使用和User模式相同…

数据治理专家岗位的能力模型

数据治理专家的角色要求其具备全方位的专业素养与技能&#xff0c;不仅要有深厚的业务理解与数据技术功底&#xff0c;还需展现出卓越的领导力、团队协作与沟通能力&#xff0c;以驱动组织内部数据治理工作的高效运行与持续优化。以下是对数据治理专家各项能力的深入解读&#…