Centos 7.x 配置PostgreSQL自启动
admin
2023-05-21 13:22:39
0

PostgreSQL starts with the system

The document describe how to configure automatic start PostgreSQL when the system starts.

The ways suitable for  systemd  on CentOS 7.0 or latest release.


1.  Create and configure postgresql.service

# vim /usr/lib/systemd/system/postgresql.service

[Unit]  

    Description=PostgreSQL database server

    After=remote-fs.target nss-lookup.target  

    After=network.target sshd.service

    After=proc-fs-nfsd.mount

    After=network.target local-fs.target

    After=nfs-config.service

    After=nfs-mountd.service

    After=nfs-blkmap.service

    After=nfs-client.target

    After=nfs-config.service

    After=nfs-idmapd.service

    After=nfs-idmap.service

    After=nfs-lock.service

    After=nfslock.service

    After=nfs-mountd.service

    After=nfs-secure-server.service

    After=nfs-secure.service

    After=nfs.service

    After=nfs-utils.service

[Service]  

    Type=forking  

    User=postgres

    Group=appuser

    Environment=PGPORT=5432  

    Environment=PGDATA=/data/01/local/pgsql/data  

    ExecStart=/data/01/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300  

    ExecStop=/data/01/local/pqsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast  

    ExecReload=/data/01/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s  

    TimeoutSec=300  

[Install]  

    WantedBy=remote-fs.target

#chmod 644 /usr/lib/systemd/system/postgresql.service


2.  Reload systemd  and enable postgresql.service

#systemctl daemon-reload


#systemctl enable postgresql.service

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.


#systemctl is-enabled postgresql.service

Enabled


#systemctl start postgresql.service


#systemctl status postgresql.service

● postgresql.service - PostgreSQL database server

   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)

   Active: active (running) since Fri 2018-02-23 22:48:49 KST; 28min ago

Main PID: 2506 (postgres)

   CGroup: /system.slice/postgresql.service

           ├─2506 /data/01/local/pgsql/bin/postgres -D /data/01/local/pgsql/data -p 5432

           ├─2508 postgres: logger process   

           ├─2510 postgres: checkpointer process   

           ├─2511 postgres: writer process   

           ├─2512 postgres: wal writer process   

           ├─2513 postgres: autovacuum launcher process   

           └─2514 postgres: stats collector process   

Feb 23 22:48:47 ec5d-pbfcompilation-02 systemd[1]: Starting PostgreSQL database server...

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

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".

Feb 23 22:48:49 ec5d-pbfcompilation-02 systemd[1]: Started PostgreSQL database server.


Note: CentOS7.x not support /etc/rc.local script file, Detailed description as following:

#cat /etc/rc.local

#!/bin/bash

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

#

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

#这是明智的在系统重启的时候,去创建一个systemd 服务或者udev规则去运行脚本,而不是用/etc/rc.local

#

# In contrast to previous versions due to parallel execution during boot

# this script will NOT be run after all other services.

#与先前的系统版本相比较,在系统启动的时候,由于服务是并行运行的,所以在其他服务启动之后,/etc/rc.local不会运行

#

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure

# that this script will be executed during boot.

虽然这里说可以使用chmod  +x /etc/rc.local 去执行这个脚本,但是笔者在这个文件中添加一行:su - postgres -c ‘pg_ctl start -D /usr/local/pgsql/data’

然后reboot测试了多次,postgres最后都没有随系统启动成功,所以不再推荐(官方也不推荐了)使用/etc/rc.local文件的方式启动postgres,启动其他服务也一样,最好配置一个systemd服务。




相关内容

热门资讯

差一点,世界又上演一场交叉战争 真就差那么一点,世界又上演一场凶险的交叉战争。之所以说是“交叉”,是因为本来两场孤立的战争,突然勾连...
中国规范出入境政策,西方在“担... 导读:近年来,越来越多的外国友人通过实地走访、亲身体验,看到了一个与西方媒体长期抹黑所塑造的形象截然...
3名儿童遭乌袭击身亡,俄痛批北... 据凤凰卫视报道,俄罗斯外交部发言人扎哈罗娃8月3日就乌克兰军队袭击克拉斯诺达尔边疆区一事表示,北约国...
印度大量驳回对华反倾销调查建议 【环球时报报道 记者 倪浩】印度正不断调整政策走向,释放出改善对华经贸关系的信号。《印度教徒报》8月...
原创 美... 文|追梦人 前言 7月31日,《国务院关于出境入境管理的规定》正式公布。 一条看似专业的内容很快引发...
泽连斯基签署总统令,解除多名乌... △斯特凡尼希娜(资料图)当地时间8月3日,乌克兰总统泽连斯基签署总统令,解除了乌克兰驻美国大使斯特凡...
10级美颜证件照火出圈的精神科... 极目新闻记者 王鹏近日,山西医科大学第一医院精神科医生郎小娥的证件照,因美颜效果突出火爆出圈,引发热...
全美25州提起诉讼,反对美政府... △美国白宫(资料图)央视记者当地时间8月3日获悉,法庭文件显示,美国有25个州提起诉讼,反对美政府最...
小米澎程72小时一线预售快报出... 来源:CNMO科技 【CNMO科技消息】8月3日,CNMO科技注意到,车fans创始人@孙少军发布了...
海评面:“中国商业航天正以惊人... 7月22日,引力一号遥四运载火箭在上海东部海域点火升空。(图/新华社) “中国商业航天领域正以惊人的...