PostgreSQL数据库启动和关闭

来源:这里教程网 时间:2026-03-14 20:44:55 作者:

PostgreSQL启动: 一:postgres启动

[postgres@cjc-db-01 ~]$postgres -D /pg/data &

相关参数,见帮助信息

[postgres@cjc-db-01 ~]$ postgres --help

二:pg_ctl启动 指定数据路径和数据文件 

pg_ctl -D /pg/data -l /pg/log/pg.log start

查看pg_ctl命令帮助信息 pg_ctl工具主要用于PostgreSQL server 初始化、启动、停止、重启等操作。

[postgres@cjc-db-01 ~]$ 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 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 QUIT TERM USR1 USR2
Report bugs to <pgsql-bugs@postgresql.org>.

PostgreSQL关闭: 一:直接向运行的postgres主进程发送signal信号,停止数据库 二:使用pg_ctl命令停止数据库 1.smart 智能关机模式

quit after all clients have disconnected

在接收到关库请求后,不在允许新的连接,等已有连接全部结束后才关闭数据库,速度慢。 命令:

pg_ctl -D /pg/data -l /pg/log/pg.log stop -m smart

对应日志:

2023-01-18 14:37:17.820 CST [2711] LOG:  received smart shutdown request
2023-01-18 14:37:31.907 CST [2711] LOG:  worker process: logical replication launcher (PID 2718) exited with exit code 1
2023-01-18 14:37:31.914 CST [2713] LOG:  shutting down
2023-01-18 14:37:31.990 CST [2711] LOG:  database system is shut down

2.fast 快速关闭模式

quit directly, with proper shutdown (default)

不在允许新的连接,向所有活跃服务进程发送SIGTERM信号,让它们立即退出,然后等待所有子进程退出并关闭数据库。 命令:

pg_ctl -D /pg/data -l /pg/log/pg.log stop -m fast

不指定-m参数,默认就是fast

pg_ctl -D /pg/data -l /pg/log/pg.log stop

对应日志:

2023-01-18 14:38:33.444 CST [24347] LOG:  received fast shutdown request
2023-01-18 14:38:33.454 CST [24347] LOG:  aborting any active transactions
2023-01-18 14:38:33.469 CST [24347] LOG:  worker process: logical replication launcher (PID 24354) exited with exit code 1
2023-01-18 14:38:33.472 CST [24349] LOG:  shutting down
2023-01-18 14:38:33.525 CST [24347] LOG:  database system is shut down

3.immediate 立即关闭模式

quit without complete shutdown; will lead to recovery on restart

主进程postgres向所有子进程发送SIGQUIT信号,并立即退出,所有子进程也会立即退出,下次启动时数据库会重放WAL日志进行恢复。 命令:

pg_ctl -D /pg/data -l /pg/log/pg.log stop -m immediate

对应日志:

2023-01-18 14:40:24.574 CST [24491] LOG:  received immediate shutdown request
2023-01-18 14:40:24.600 CST [24496] WARNING:  terminating connection because of crash of another server process
2023-01-18 14:40:24.600 CST [24496] DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2023-01-18 14:40:24.600 CST [24496] HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2023-01-18 14:40:24.607 CST [24491] LOG:  database system is shut down

查看启动日志,执行了自动恢复

2023-01-18 14:41:03.117 CST [24553] LOG:  database system was interrupted; last known up at 2023-01-18 14:40:10 CST
2023-01-18 14:41:03.978 CST [24553] LOG:  database system was not properly shut down; automatic recovery in progress
2023-01-18 14:41:03.982 CST [24553] LOG:  invalid record length at 0/171C9B0: wanted 24, got 0
2023-01-18 14:41:03.982 CST [24553] LOG:  redo is not required
2023-01-18 14:41:04.009 CST [24552] LOG:  database system is ready to accept connections

###chenjuchao 20230118 15:20###

相关推荐