如何使用systemctl管理postgres数据库启停

   当我们使用源码编译安装PostgreSQL数据库后,通常情况下是没有直接可使用的systemctl管理,若需要使用systemctl进行服务的管理,可进行以下配置:

环境:CentOS 7
数据库:PostgreSQL 11
systemctl管理脚本:/usr/lib/systemd/system/postgres11.service
systemctl管理命令:systemctl {status|restart|start|stop|reload} postgres11

具体操作如下:

-- 进入指定目录
[root@172-16-104-7 system]# pwd
/usr/lib/systemd/system

-- 编写systemctl服务脚本 ,
[root@172-16-104-7 system]# cat postgres11.service 
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  It is recommended to use systemd
# "dropin" feature;  i.e. create file with suffix .conf under
# /etc/systemd/system/postgresql-11.service.d directory overriding the
# unit's defaults. You can also use "systemctl edit postgresql-11"
# Look at systemd.unit(5) manual page for more info.

# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
[Unit]
Description=PostgreSQL 11 database server
Documentation=https://www.postgresql.org/docs/11/static/
After=syslog.target
After=network.target

[Service]
Type=notify

User=postgres																												# 数据库启动属主
Group=postgres																												# 数据库启动属组

# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.

# Location of database directory
Environment=PGDATA=/pg_data/pgsql11/data/																# 数据库数据目录
Environment=PGPORT=5432																						# 数据库端口

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
#OOMScoreAdjust=-1000
#Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
#Environment=PG_OOM_ADJUST_VALUE=0

ExecStart=/usr/local/pgsql11/bin/postmaster -D ${PGDATA}											# start 对应命令
ExecStop=/usr/local/pgsql11/bin/pg_ctl stop -D ${PGDATA} -s -m fast                          # stop 对应命令     
# ExecReload=/usr/local/pgsql11/bin/pg_ctl reload -D ${PGDATA} -s                               
ExecReload=/bin/kill -HUP $MAINPID                                                                            # reload 对应命令

KillMode=mixed
KillSignal=SIGINT


# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
# TimeoutSec=0
TimeoutSec=300

[Install]
WantedBy=multi-user.target

-- 服务启停和状态检查
[root@172-16-104-7 system]# systemctl restart postgres11
[root@172-16-104-7 system]# 
[root@172-16-104-7 system]# systemctl status postgres11
● postgres11.service - PostgreSQL 11 database server
   Loaded: loaded (/usr/lib/systemd/system/postgres11.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-05-14 16:20:14 CST; 8s ago
     Docs: https://www.postgresql.org/docs/11/static/
  Process: 38896 ExecStop=/usr/local/pgsql11/bin/pg_ctl stop -D ${PGDATA} -s -m fast (code=exited, status=0/SUCCESS)
 Main PID: 38898 (postmaster)
   CGroup: /system.slice/postgres11.service
           ├─38898 /usr/local/pgsql11/bin/postmaster -D /pg_data/pgsql11/data/
           ├─38899 postgres: logger   
           ├─38900 postgres: startup   recovering 0000000A000000000000002D
           ├─38901 postgres: checkpointer   
           ├─38902 postgres: background writer   
           ├─38903 postgres: stats collector   
           └─38904 postgres: walreceiver   

May 14 16:20:14 172-16-104-7 systemd[1]: Starting PostgreSQL 11 database server...
May 14 16:20:14 172-16-104-7 postmaster[38898]: 2021-05-14 16:20:14.893 CST [38898] LOG:  listening on IPv4 address "0.0.0.0", port 5432
May 14 16:20:14 172-16-104-7 postmaster[38898]: 2021-05-14 16:20:14.893 CST [38898] LOG:  listening on IPv6 address "::", port 5432
May 14 16:20:14 172-16-104-7 postmaster[38898]: 2021-05-14 16:20:14.896 CST [38898] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
May 14 16:20:14 172-16-104-7 postmaster[38898]: 2021-05-14 16:20:14.917 CST [38898] LOG:  redirecting log output to logging collector process
May 14 16:20:14 172-16-104-7 postmaster[38898]: 2021-05-14 16:20:14.917 CST [38898] HINT:  Future log output will appear in directory "/pg_data/pgsql11/logs".
May 14 16:20:14 172-16-104-7 systemd[1]: Started PostgreSQL 11 database server.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值