参考:
https://www.postgresql.org/docs/11/server-start.html#SERVER-START-FAILURES
https://www.postgresql.org/docs/11/reference-server.html
总结:
1、前台启动 postgres或者postmaster
2、后台启动 pg_ctl
先来说一说,何谓前台启动,可以从以下的演示看出,单纯的使用postgres命令启动数据库,当前会话终止或者断开后数据库就会自动关闭。
[postgresql@tidb63 ~]$ /usr/local/pgsql/bin/postgres 2020-08-19 17:00:13.393 CST [122585] LOG: listening on IPv4 address "0.0.0.0", port 5432 2020-08-19 17:00:13.393 CST [122585] LOG: listening on IPv6 address "::", port 5432 2020-08-19 17:00:13.394 CST [122585] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2020-08-19 17:00:13.406 CST [122586] LOG: database system was shut down at 2020-08-19 16:59:12 CST 2020-08-19 17:00:13.412 CST [122585] LOG: database system is ready to accept connections ^C2020-08-19 17:00:32.767 CST [122585] LOG: received fast shutdown request 2020-08-19 17:00:32.767 CST [122585] LOG: aborting any active transactions 2020-08-19 17:00:32.767 CST [122585] LOG: background worker "logical replication launcher" (PID 122593) exited with exit code 1 2020-08-19 17:00:32.767 CST [122587] LOG: shutting down 2020-08-19 17:00:32.826 CST [122585] LOG: database system is shut down [postgresql@tidb63 ~]$
所以,如果要使用postgres命令启动PostgreSQL实例,需要放在使用"&" 放在后台执行。
[postgresql@tidb63 ~]$ nohup /usr/local/pgsql/bin/postgres -D $PGDATA & [1] 130040 [postgresql@tidb63 ~]$ nohup: 忽略输入并把输出追加到"nohup.out" [postgresql@tidb63 ~]$ ps -ef | grep postgres | egrep -v 'grep|su' postgre+ 130040 90407 0 17:20 pts/4 00:00:00 /usr/local/pgsql/bin/postgres -D /home/jm/pg118/pg5432/data postgre+ 130042 130040 0 17:20 ? 00:00:00 postgres: checkpointer postgre+ 130043 130040 0 17:20 ? 00:00:00 postgres: background writer postgre+ 130044 130040 0 17:20 ? 00:00:00 postgres: walwriter postgre+ 130045 130040 0 17:20 ? 00:00:00 postgres: autovacuum launcher postgre+ 130046 130040 0 17:20 ? 00:00:00 postgres: archiver postgre+ 130047 130040 0 17:20 ? 00:00:00 postgres: stats collector postgre+ 130048 130040 0 17:20 ? 00:00:00 postgres: logical replication launcher [postgresql@tidb63 ~]$ pg_ctl stop -D $PGDATA -m fast waiting for server to shut down.... done server stopped [1]+ 完成 nohup /usr/local/pgsql/bin/postgres -D $PGDATA
从以上可以看出前台命令启动的pg,仍然可以使用pg_ctl命令进行关闭。
可以看出postmaster其实是postgres的一个软链接。
[postgresql@tidb63 bin]$ pwd /usr/local/pgsql/bin [postgresql@tidb63 bin]$ ls -l post* -rwxr-xr-x. 1 root root 7931144 7月 28 16:37 postgres lrwxrwxrwx. 1 root root 8 7月 28 16:37 postmaster -> postgres [postgresql@tidb63 bin]$ cd [postgresql@tidb63 ~]$ nohup /usr/local/pgsql/bin/postmaster -D $PGDATA & [1] 131283 [postgresql@tidb63 ~]$ nohup: 忽略输入并把输出追加到"nohup.out" [postgresql@tidb63 ~]$ ps -ef | grep postgres | egrep -v 'grep|su' postgre+ 131285 131283 0 17:24 ? 00:00:00 postgres: checkpointer postgre+ 131286 131283 0 17:24 ? 00:00:00 postgres: background writer postgre+ 131287 131283 0 17:24 ? 00:00:00 postgres: walwriter postgre+ 131288 131283 0 17:24 ? 00:00:00 postgres: autovacuum launcher postgre+ 131289 131283 0 17:24 ? 00:00:00 postgres: archiver postgre+ 131290 131283 0 17:24 ? 00:00:00 postgres: stats collector postgre+ 131291 131283 0 17:24 ? 00:00:00 postgres: logical replication launcher [postgresql@tidb63 ~]$ pg_ctl stop -D $PGDATA -m fast waiting for server to shut down.... done server stopped [1]+ 完成 nohup /usr/local/pgsql/bin/postmaster -D $PGDATA [postgresql@tidb63 ~]$ [postgresql@tidb63 ~]$ nohup /usr/local/pgsql/bin/postmaster -D $PGDATA & [1] 131283 [postgresql@tidb63 ~]$ nohup: 忽略输入并把输出追加到"nohup.out"
配置systemd服务启停
[root@tidb63 system]# pwd /usr/lib/systemd/system [root@tidb63 system]# cat pg118.service [Unit] Description=PostgreSQL 11.8 database server [Service] Type=forking User=postgresql ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /home/jm/pg118/pg5432/data -l /tmp/logfile ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /home/jm/pg118/pg5432/data -l /tmp/logfile TimeoutSec=0 [Install] WantedBy=multi-user.target
说明:官方文档中提到,Using Type=notify
requires that the server binary was built with configure --with-systemd
.
测试的时候,如果使用的是pg_ctl后台启动,编译的时候并没有指定---with-systemd,也是可以用服务进行管理的,注意需要安装:systemd-devel.x86_64
[postgresql@tidb63 ~]$ pg_config --configure '--prefix=/usr/local/pgsql' '--with-pgport=5432' [postgresql@tidb63 ~]$
[root@tidb63 system]# systemctl daemon-reload [root@tidb63 system]# systemctl start pg118.service [root@tidb63 system]# systemctl status pg118.service ● pg118.service - PostgreSQL 11.8 database server Loaded: loaded (/usr/lib/systemd/system/pg118.service; disabled; vendor preset: disabled) Active: active (running) since 四 2020-08-20 13:41:06 CST; 1s ago Process: 148181 ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /home/jm/pg118/pg5432/data -l /tmp/logfile (code=exited, status=0/SUCCESS) Main PID: 148183 (postgres) Tasks: 8 Memory: 13.4M CGroup: /system.slice/pg118.service ├─148183 /usr/local/pgsql/bin/postgres -D /home/jm/pg118/pg5432/data ├─148185 postgres: checkpointer ├─148186 postgres: background writer ├─148187 postgres: walwriter ├─148188 postgres: autovacuum launcher ├─148189 postgres: archiver ├─148190 postgres: stats collector └─148191 postgres: logical replication launcher 8月 20 13:41:06 tidb63.com systemd[1]: Starting PostgreSQL 11.8 database server... 8月 20 13:41:06 tidb63.com systemd[1]: Started PostgreSQL 11.8 database server. [root@tidb63 system]# [root@tidb63 system]# systemctl stop pg118.service [root@tidb63 system]# systemctl status pg118.service ● pg118.service - PostgreSQL 11.8 database server Loaded: loaded (/usr/lib/systemd/system/pg118.service; disabled; vendor preset: disabled) Active: inactive (dead) 8月 20 11:58:01 tidb63.com pg_ctl[117065]: 2020-08-20 11:58:01.449 CST [117067] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 8月 20 11:58:01 tidb63.com pg_ctl[117065]: 2020-08-20 11:58:01.464 CST [117068] LOG: database system was shut down at 2020...46 CST 8月 20 11:58:01 tidb63.com pg_ctl[117065]: 2020-08-20 11:58:01.466 CST [117067] LOG: database system is ready to accept connections 8月 20 11:58:01 tidb63.com systemd[1]: Started PostgreSQL 11.8 database server. 8月 20 13:40:39 tidb63.com systemd[1]: Stopping PostgreSQL 11.8 database server... 8月 20 13:40:39 tidb63.com systemd[1]: Stopped PostgreSQL 11.8 database server. 8月 20 13:41:06 tidb63.com systemd[1]: Starting PostgreSQL 11.8 database server... 8月 20 13:41:06 tidb63.com systemd[1]: Started PostgreSQL 11.8 database server. 8月 20 13:41:30 tidb63.com systemd[1]: Stopping PostgreSQL 11.8 database server... 8月 20 13:41:30 tidb63.com systemd[1]: Stopped PostgreSQL 11.8 database server. Hint: Some lines were ellipsized, use -l to show in full. [root@tidb63 system]#
如果使用systemctl服务器启动了数据库,之后又在postgresql用户下使用pg_ctl关闭实例,再查看服务状态会出现异常。
[root@tidb63 system]# systemctl status pg118.service ● pg118.service - PostgreSQL 11.8 database server Loaded: loaded (/usr/lib/systemd/system/pg118.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since 四 2020-08-20 13:44:18 CST; 7s ago Process: 149196 ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /home/jm/pg118/pg5432/data -l /tmp/logfile (code=exited, status=1/FAILURE) Process: 148969 ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /home/jm/pg118/pg5432/data -l /tmp/logfile (code=exited, status=0/SUCCESS) Main PID: 148971 (code=exited, status=0/SUCCESS) 8月 20 13:43:42 tidb63.com systemd[1]: Starting PostgreSQL 11.8 database server... 8月 20 13:43:42 tidb63.com systemd[1]: Started PostgreSQL 11.8 database server. 8月 20 13:44:18 tidb63.com systemd[1]: pg118.service: control process exited, code=exited status=1 8月 20 13:44:18 tidb63.com systemd[1]: Unit pg118.service entered failed state. 8月 20 13:44:18 tidb63.com systemd[1]: pg118.service failed. [root@tidb63 system]#
所以,启停pg,全部使用systemctl服务,或者全部使用pg_ctl/postgres/postmaster,不要混用。