1、查找Postgresql数据库开机自启动脚本位置
[root@dragon ~]# find / -name start-scripts
/opt/postgres/postgresql-9.4.12/contrib/start-scripts
[root@dragon ~]# cd /opt/postgres/postgresql-9.4.12/contrib/start-scripts
[root@dragon start-scripts]# ll
总用量 8
-rw-r--r-- 1 1107 1107 1529 5月 9 2017 freebsd
-rw-r--r-- 1 1107 1107 3437 5月 9 2017 linux
drwxrwxrwx 2 1107 1107 87 5月 9 2017 osx
# linux文件为启动脚本文件
2、将linux文件复制到 /etc/init.d 目录下,并且将其重名为postgresql
[root@dragon start-scripts]# cp linux /etc/init.d/postgresql
3、进入 /etc/init.d 目录下,修改postgresql文件
[root@dragon start-scripts]# cd /etc/init.d/
[root@dragon init.d]# ls
functions grafana-server jexec netconsole network postgresql README
[root@dragon init.d]# vim postgresql
将prefix设置为postgresql的安装路径:/usr/local/postgresql
将PGDATA设置为postgresql的数据目录路径:/usr/local/postgresql/data
4、添加到开机启动
[root@dragon init.d]# chomd +x postgresql
[root@dragon init.d]# chkconfig --add postgresql
[root@dragon init.d]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
jexec 0:关1:开2:开3:开4:开5:开6:关
netconsole 0:关1:关2:关3:关4:关5:关6:关
network 0:关1:关2:开3:开4:开5:开6:关
postgresql 0:关1:关2:开3:开4:开5:开6:关
5、重启服务器测试
[root@dragon init.d]# reboot
[root@dragon init.d]# systemctl status postgresql
● postgresql.service - SYSV: PostgreSQL RDBMS
Loaded: loaded (/etc/rc.d/init.d/postgresql; bad; vendor preset: disabled)
Active: active (exited) since 一 2022-10-10 10:53:31 CST; 44min ago
Docs: man:systemd-sysv-generator(8)
Process: 941 ExecStart=/etc/rc.d/init.d/postgresql start (code=exited, status=0/SUCCESS)
10月 10 10:53:31 dragon systemd[1]: Starting SYSV: PostgreSQL RDBMS...
10月 10 10:53:31 dragon su[943]: (to postgres) root on none
10月 10 10:53:31 dragon systemd[1]: Started SYSV: PostgreSQL RDBMS.
10月 10 10:53:31 dragon postgresql[941]: Starting PostgreSQL: ok