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服务。




相关内容

热门资讯

专家谈柳州地震原因:处于两条断... 据凤凰卫视报道,5月17日以来,广西柳州陆续发生至少3次地震,震级分别为2.5级、3.2级和5.2级...
国民党青年:特朗普最新表态对台... 中国国民党前青年部主任陈冠安接受香港中评社访问表示,特朗普访华虽然没有直接讲出反对“台独”四个字,但...
美媒曝美军对伊特战计划:自带挖... 美媒日前称,五角大楼正在为恢复对伊朗的军事行动做准备,美国和以色列最早可能在本周重启对伊朗的军事打击...
通话老人手表厂家推荐指南:下沉... 导语:随着智能穿戴设备在健康监测领域的渗透率持续提升,下沉市场对适老化健康智能手表的需求呈现爆发式增...
广西皮卡车过桥坠河,已致5死5... 记者从广西环江“5·16”过桥车辆坠河事件现场搜救队伍了解到,18日17时左右,搜救队伍在车辆坠河地...
缅甸提出打击电诈新法草案,最高... 据凤凰卫视报道,缅甸政府近日公布打击跨境网络诈骗的新法律草案,对经营诈骗园区与相关犯罪行为祭出重刑,...
红夹克、机舱合影,李显龙晒访华... 5月18日,新加坡国务资政李显龙在社交平台发文,他已抵达广西南宁,开启本周访华行程,并晒出机舱照、与...
“赌王”女儿何超蕸,真正死因公... 5月17日,已故“赌王”何鸿燊女儿何超蕸的追思会,在香港举行。此次追思会由何超琼亲自操办,一早她便到...
宇通4款高端车型亮相米兰交通运... 近日,2026米兰交通运输展览会(NME)在意大利米兰国际展览中心拉开帷幕。在这场汇聚全球目光的行业...
伊朗已逮捕6500多名间谍及通... 据伊朗迈赫尔通讯社18日报道,伊朗治安部队司令艾哈迈德-礼萨·拉丹说,自2月底美国和以色列对伊朗发动...