天天看點

PostgreSQL的設定開機自啟動

概述

為保證服務可靠性,資料庫一般都要求在伺服器啟動時自動運作,pg也需要一定設定才能實作此功能,下面就檢查測試介紹下postgresql(edb) 實作開機自啟動的兩種簡單方法

測試環境

系統

[ppas@stephen ~]$ cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)           

資料庫版本

[ppas@stephen ~]$ psql --version
psql (EnterpriseDB) 9.5.0.5           

systemcetl 服務實作

使用systemctl 服務控制實作開機自啟動

在/etc/systemd/system/ 檔案夾下,建立一個 .server 檔案,檔案内容如下,主要分為[unit],[service],[install] 三個小節

[Unit]
Description=postgresql project     #描述
After=pgserver.service            #在此服務啟動後才啟動,這裡可以不寫

[Service]
Type=forking      #服務的類型,常用的有 simple(預設類型) 和 forking。預設的 simple 類型可以适應于絕大多數的場景,是以一般可以忽略這個參數的配置。而如果服務程式啟動後會通過 fork 系統調用建立子程序,然後關閉應用程式本身程序的情況,則應該将 Type 的值設定為 forking,否則 systemd 将不會跟蹤子程序的行為,而認為服務已經退出。 pg需要通過fork來建立一些子程序,是以這裡選擇forKing
User=ppas      
Group=ppas
ExecStart=/opt/PostgresPlus/9.5AS/bin/pg_ctl start -D /opt/PostgresPlus/9.5AS/data     # 啟動指令
ExecReload=/opt/PostgresPlus/9.5AS/bin/pg_ctl restart -D /opt/PostgresPlus/9.5AS/data  # 重新加載
ExecStop=/opt/PostgresPlus/9.5AS/bin/pg_ctl stop -D /opt/PostgresPlus/9.5AS/data  # 停止,以上三指令都需要絕對路徑
PrivateTmp=true  #是否給服務配置設定獨立的臨時空間(true/false),要給的

[Install]
WantedBy=multi-user.target   #和前面的 Wants 作用相似,隻是後面列出的不是服務所依賴的子產品,而是依賴目前服務的子產品。“WantedBy=multi-user.target” 表明當系統以多使用者方式(預設的運作級别)啟動時,這個服務需要被自動運作。當然還需要 systemctl enable 激活這個服務以後自動運作才會生效           

更新設定

[root@stephen system]# systemctl daemon-reload           

啟動服務

[root@stephen system]# systemctl start pgserver.service
[root@stephen system]# 

成功後可以看到服務已經啟動程序
[ppas@stephen ~]$ ps -ef|grep postgres
ppas 31028 1 0 14:42 ? 00:00:00 /opt/PostgresPlus/9.5AS/bin/edb-postgres -D /opt/PostgresPlus/9.5AS/data
ppas 31029 31028 0 14:42 ? 00:00:00 postgres: logger process   
ppas 31031 31028 0 14:42 ? 00:00:00 postgres: checkpointer process   
ppas 31032 31028 0 14:42 ? 00:00:00 postgres: writer process   
ppas 31033 31028 0 14:42 ? 00:00:00 postgres: wal writer process   
ppas 31034 31028 0 14:42 ? 00:00:00 postgres: autovacuum launcher process   
ppas 31035 31028 0 14:42 ? 00:00:00 postgres: stats collector process   
ppas 31104 30951 0 14:50 pts/1 00:00:00 vim postgresql.conf
ppas 31194 31152 0 14:51 pts/2 00:00:00 grep --color=auto postgres
[ppas@stephen ~]$ 

[ppas@stephen ~]$ psql -h 127.0.0.1 -Uppas -d kmtest -p5432
Password for user ppas: 
psql.bin (9.5.0.5)
Type "help" for help.

kmtest=#                           

這裡在測試過程中,發現一個小問題,使用systemctl啟動的資料庫,并沒有在/tmp 下面建立socket檔案,在本地登入時會失敗,必須要使用 -h  指定好host才能正常登入,這點倒是問題不大                                                                                                              

配置開機自啟動

[root@stephen system]# systemctl enable pgserver.service
Created symlink from /etc/systemd/system/multi-user.target.wants/pgserver.service to /etc/systemd/system/pgserver.service.
[root@stephen system]#            

測試重新開機,已經開機自啟動了

[root@stephen system]# reboot
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(2020_pgtest) at 15:09:17.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ 

[root@stephen ~]# ps -ef|grep postgres
ppas 687 1 0 15:09 ? 00:00:00 /opt/PostgresPlus/9.5AS/bin/edb-postgres -D /opt/PostgresPlus/9.5AS/data
ppas 710 687 0 15:09 ? 00:00:00 postgres: logger process   
ppas 721 687 0 15:09 ? 00:00:00 postgres: checkpointer process   
ppas 722 687 0 15:09 ? 00:00:00 postgres: writer process   
ppas 723 687 0 15:09 ? 00:00:00 postgres: wal writer process   
ppas 724 687 0 15:09 ? 00:00:00 postgres: autovacuum launcher process   
ppas 725 687 0 15:09 ? 00:00:00 postgres: stats collector process   
root 1197 1153 0 15:10 pts/0 00:00:00 grep --color=auto postgres           

自帶腳本實作

另外,如果不想用這種服務,或者centos6的,可以試試使用pg源碼自帶的啟動腳本來實作自啟動功能

在源碼包的/contrib/start-scripts/ 中的Linux腳本(具體腳本内容可見檔案結尾附件)

主要需要修改一下以下參數

# Installation prefix
prefix=/opt/PostgresPlus/9.5AS

# Data directory
PGDATA="/opt/PostgresPlus/9.5AS/data"

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

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

DAEMON="$prefix/bin/postmaster"    #這個參數注意下,我安裝的edb是叫edb-postmaster ,一般的pg都隻是叫postmaster           

複制檔案+賦權

[root@stephen ~]# cp linux /etc/init.d/ppas
[root@stephen ~]# chmod a+x /etc/init.d/ppas            

測試使用服務啟動資料庫

[root@stephen init.d]# service ppas start
Restarting PostgreSQL: ok

[root@stephen init.d]# service ppas stop
Stopping PostgreSQL: ok           

設定開機自啟動

[root@stephen init.d]# chkconfig --add ppas
[root@stephen init.d]# chkconfig --list ppas

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

ppas 0:off    1:off    2:on    3:on    4:on    5:on    6:off           

使用這種方法也能配置pg服務的開機自啟動

附錄

pg源碼中自帶的啟動腳本:

#! /bin/sh

# chkconfig: 2345 98 02
# description: PostgreSQL RDBMS

# This is an example of a start/stop script for SysV-style init, such
# as is used on Linux systems. You should edit some of the variables
# and maybe the 'echo' commands.
#
# Place this file at /etc/init.d/postgresql (or
# /etc/rc.d/init.d/postgresql) and make symlinks to
# /etc/rc.d/rc0.d/K02postgresql
# /etc/rc.d/rc1.d/K02postgresql
# /etc/rc.d/rc2.d/K02postgresql
# /etc/rc.d/rc3.d/S98postgresql
# /etc/rc.d/rc4.d/S98postgresql
# /etc/rc.d/rc5.d/S98postgresql
# Or, if you have chkconfig, simply:
# chkconfig --add postgresql
#
# Proper init scripts on Linux systems normally require setting lock
# and pid files under /var/run as well as reacting to network
# settings, so you should treat this with care.

# Original author: Ryan Kirkpatrick <[email protected]>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation prefix
prefix=/opt/PostgresPlus/9.5AS

# Data directory
PGDATA="/opt/PostgresPlus/9.5AS/data"

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

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

# It's often a good idea to protect the postmaster from being killed by the
# OOM killer (which will tend to preferentially kill the postmaster because
# of the way it accounts for shared memory). To do that, uncomment these
# three lines:
#PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
#PG_MASTER_OOM_SCORE_ADJ=-1000
#PG_CHILD_OOM_SCORE_ADJ=0
# Older Linux kernels may not have /proc/self/oom_score_adj, but instead
# /proc/self/oom_adj, which works similarly except for having a different
# range of scores. For such a system, uncomment these three lines instead:
#PG_OOM_ADJUST_FILE=/proc/self/oom_adj
#PG_MASTER_OOM_SCORE_ADJ=-17
#PG_CHILD_OOM_SCORE_ADJ=0

## STOP EDITING HERE

# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# What to use to start up the postmaster. (If you want the script to wait
# until the server has started, you could use "pg_ctl start" here.)
DAEMON="$prefix/bin/postmaster"

# What to use to shut down the postmaster
PGCTL="$prefix/bin/pg_ctl"

set -e

# Only start if we can find the postmaster.
test -x $DAEMON ||
{
 echo "$DAEMON not found"
 if [ "$1" = "stop" ]
 then exit 0
 else exit 5
 fi
}

# If we want to tell child processes to adjust their OOM scores, set up the
# necessary environment variables. Can't just export them through the "su".
if [ -e "$PG_OOM_ADJUST_FILE" -a -n "$PG_CHILD_OOM_SCORE_ADJ" ]
then
 DAEMON_ENV="PG_OOM_ADJUST_FILE=$PG_OOM_ADJUST_FILE PG_OOM_ADJUST_VALUE=$PG_CHILD_OOM_SCORE_ADJ"
fi


# Parse command line parameters.
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 "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
 echo "ok"
 ;;
  stop)
 echo -n "Stopping PostgreSQL: "
 su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
 echo "ok"
 ;;
  restart)
 echo -n "Restarting PostgreSQL: "
 su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
 test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
 su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
 echo "ok"
 ;;
  reload)
 echo -n "Reload PostgreSQL: "
 su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
 echo "ok"
 ;;
  status)
 su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
 ;;
  *)
 # Print help
 echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
 exit 1
 ;;
esac

exit 0