Postgresql starts with the system@H_404_3@
The document describe how to configure automatic start Postgresql when the system starts.@H_404_3@
The ways suitable forsystemdon CentOS 7.0 or latest release.@H_404_3@
@H_404_3@
1.Create and configure postgresql.service@H_404_3@
# vim /usr/lib/systemd/system/postgresql.service@H_404_3@
[Unit]@H_404_3@
Description=Postgresql database server@H_404_3@
After=remote-fs.target nss-lookup.target@H_404_3@
After=network.target sshd.service@H_404_3@
After=proc-fs-nfsd.mount@H_404_3@
After=network.target local-fs.target@H_404_3@
After=nfs-config.service@H_404_3@
After=nfs-mountd.service@H_404_3@
After=nfs-blkmap.service@H_404_3@
After=nfs-client.target@H_404_3@
After=nfs-config.service@H_404_3@
After=nfs-idmapd.service@H_404_3@
After=nfs-idmap.service@H_404_3@
After=nfs-lock.service@H_404_3@
After=nfslock.service@H_404_3@
After=nfs-mountd.service@H_404_3@
After=nfs-secure-server.service@H_404_3@
After=nfs-secure.service@H_404_3@
After=nfs.service@H_404_3@
After=nfs-utils.service@H_404_3@
[Service]@H_404_3@
Type=forking@H_404_3@
User=postgres@H_404_3@
Group=appuser@H_404_3@
Environment=PGPORT=5432@H_404_3@
Environment=PGDATA=/data/01/local/pgsql/data@H_404_3@
ExecStart=/data/01/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300@H_404_3@
ExecStop=/data/01/local/pqsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast@H_404_3@
ExecReload=/data/01/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s@H_404_3@
TimeoutSec=300@H_404_3@
[Install]@H_404_3@
WantedBy=remote-fs.target@H_404_3@
#chmod 644 /usr/lib/systemd/system/postgresql.service@H_404_3@
@H_404_3@
2.Reload systemdand enable postgresql.service@H_404_3@
#systemctl daemon-reload@H_404_3@
@H_404_3@
#systemctl enable postgresql.service@H_404_3@
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.@H_404_3@
@H_404_3@
#systemctl is-enabled postgresql.service@H_404_3@
Enabled@H_404_3@
@H_404_3@
#systemctl start postgresql.service@H_404_3@
@H_404_3@
#systemctl status postgresql.service@H_404_3@
● postgresql.service - Postgresql database server@H_404_3@
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)@H_404_3@
Active: active (running) since Fri 2018-02-23 22:48:49 KST; 28min ago@H_404_3@
Main PID: 2506 (postgres)@H_404_3@
CGroup: /system.slice/postgresql.service@H_404_3@
├─2506 /data/01/local/pgsql/bin/postgres -D /data/01/local/pgsql/data -p 5432@H_404_3@
├─2508 postgres: logger process@H_404_3@
├─2510 postgres: checkpointer process@H_404_3@
├─2511 postgres: writer process@H_404_3@
├─2512 postgres: wal writer process@H_404_3@
├─2513 postgres: autovacuum launcher process@H_404_3@
└─2514 postgres: stats collector process@H_404_3@
Feb 23 22:48:47 ec5d-pbfcompilation-02 systemd[1]: Starting Postgresql database server...@H_404_3@
Feb 23 22:48:48 ec5d-pbfcompilation-02 pg_ctl[1414]: < 2018-02-23 22:48:48.671 KST >LOG:redirecting log output to logging collector process@H_404_3@
Feb 23 22:48:48 ec5d-pbfcompilation-02 pg_ctl[1414]: < 2018-02-23 22:48:48.671 KST >HINT:Future log output will appear in directory "pg_log".@H_404_3@
Feb 23 22:48:49 ec5d-pbfcompilation-02 systemd[1]: Started Postgresql database server.@H_404_3@
@H_404_3@
Note: CentOS7.x not support /etc/rc.local script file,Detailed description as following:@H_404_3@
#cat /etc/rc.local@H_404_3@
#!/bin/bash@H_404_3@
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES@H_404_3@
#@H_404_3@
# It is highly advisable to create own systemd services or udev rules@H_404_3@
# to run scripts during boot instead of using this file.@H_404_3@
#这是明智的在系统重启的时候,去创建一个systemd 服务或者udev规则去运行脚本,而不是用/etc/rc.local@H_404_3@
#@H_404_3@
# In contrast to prevIoUs versions due to parallel execution during boot@H_404_3@
# this script will NOT be run after all other services.@H_404_3@
#与先前的系统版本相比较,在系统启动的时候,由于服务是并行运行的,所以在其他服务启动之后,/etc/rc.local不会运行@H_404_3@
#@H_404_3@
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure@H_404_3@
# that this script will be executed during boot.@H_404_3@
虽然这里说可以使用chmod +x /etc/rc.local 去执行这个脚本,但是笔者在这个文件中添加一行:su - postgres -c ‘pg_ctl start -D /usr/local/pgsql/data’@H_404_3@
然后reboot测试了多次,postgres最后都没有随系统启动成功,所以不再推荐(官方也不推荐了)使用/etc/rc.local文件的方式启动postgres,启动其他服务也一样,最好配置一个systemd服务。@H_404_3@