PostgreSQL 设置开机启动

PostgreSQL About 2,323 words

脚本

PostgreSQL源码包中自带了一个启动脚本。位于contrib/start-scripts

cd /home/postgres/postgresql-16.1/contrib/start-scripts/

start-script下的文件

[root@local start-scripts]# ll
total 12
-rw-r--r-- 1 postgres postgres 1441 Nov  7  2023 freebsd
-rw-r--r-- 1 postgres postgres 3526 Nov  7  2023 linux
drwxrwxr-x 2 postgres postgres 4096 Nov  7  2023 macos

设置开机启动

拷贝脚本

cp /home/postgres/postgresql-16.1/contrib/start-scripts/linux /etc/init.d/postgresql

编辑脚本

vim /etc/init.d/postgresql

主要参数

prefix=/home/postgres/16.1
PGDATA="/home/postgres/16.1/data"

修改权限

chmod +x /etc/init.d/postgresql

添加配置

chkconfig --add postgresql

查看配置列表

chkconfig --list

输出

[root@local ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

hostguard       0:off   1:off   2:on    3:on    4:on    5:on    6:off
multi-queue-hw  0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:off   3:off   4:off   5:off   6:off
postgresql      0:off   1:off   2:on    3:on    4:on    5:on    6:off

查看状态

备注:需重启机器reboot

systemctl status postgresql

输出

[root@ecs-74883 ~]# systemctl status postgresql
● postgresql.service - SYSV: PostgreSQL RDBMS
   Loaded: loaded (/etc/rc.d/init.d/postgresql; bad; vendor preset: disabled)
   Active: active (exited) since Sat 2024-06-22 15:18:19 CST; 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1411 ExecStart=/etc/rc.d/init.d/postgresql start (code=exited, status=0/SUCCESS)

Jun 22 15:18:19 ecs-74883 systemd[1]: Starting SYSV: PostgreSQL RDBMS...
Jun 22 15:18:19 ecs-74883 su[1415]: (to postgres) root on none
Jun 22 15:18:19 ecs-74883 postgresql[1411]: Starting PostgreSQL: ok
Jun 22 15:18:19 ecs-74883 systemd[1]: Started SYSV: PostgreSQL RDBMS.

也可以使用pg_ctl查看运行状态

sudo -u postgres /home/postgres/16.1/bin/pg_ctl status
[root@local ~]# sudo -u postgres /home/postgres/16.1/bin/pg_ctl status
pg_ctl: server is running (PID: 1433)
/home/postgres/16.1/bin/postgres "-D" "/home/postgres/16.1/data"
Views: 648 · Posted: 2024-06-23

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh