将postgresql配置为Linux服务管理 systemctl service

发布于:2023-09-14 ⋅ 阅读:(122) ⋅ 点赞:(0)

       发行版安装的数据库在装完后会自动添加数据库服务,但编译安装的不会,需要手动添加。

cd postgresql-14.0/contrib/start-scripts/
cp linux /etc/init.d/postgresql
cd /etc/init.d/

vi postgresql

修改以下部分

## EDIT FROM HERE

# Installation prefix
prefix="/data/postgres/base/14.0"

# Data directory
PGDATA="/data/postgres/pg5432/data"

# Who to run the postmaster as, usually "postgres".  (NOT "root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$PGDATA/log"

再改下启动命令

case $1 in
  start)
        echo -n "Starting PostgreSQL: "
        test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
        su - $PGUSER -c "$PGCTL start -D '$PGDATA' -s"
        echo "ok"
        ;;

加执行权限   chmod +x postgresql

通过下面的命令添加到服务中 

chkconfig --add postgresql

否则会报错

[root@localhost init.d]# systemctl stop postgresql
Failed to stop postgresql.service: Unit postgresql.service not loaded.

相应的,可通过下述命令删除

chkconfig --del postgresql

如果后面修改了postgresql文件,需要reload

systemctl daemon-reload

启动测试

systemctl start postgresql 
systemctl status postgresql   
ps -ef|grep postgres

关闭测试

systemctl stop postgresql
ps -ef|grep postgres

参考

PGCE课程《服务管理》 


网站公告

今日签到

点亮在社区的每一天
去签到