[postgres]$ killall postgres
・起動スクリプトコピー
[root]# cp -v /usr/local/src/postgresql-x.x.x/contrib/start-script/linux
/etc/rc.d/init.d/postgresql
[root]# vim /etc/rc.d/init.d/postgresql
chkconfig: 35 98 02 (起動ランレベルを 3,5 に変更)
・パーミッション変更
[root]# chmod 755 /etc/rc.d/init.d/postgresql
・起動/停止
[root]# service postgresql { start stop reload restart status }
・自動起動/停止
[root]# chkconfig --add postgresql
[root]# chkconfig postgresql { on off }
[postgres]$ killall postgres
Sysvinit の init プロセス用リンクを作成、削除。サービスをソースインストールした場合、設定が必要。
[root]# vim /etc/init.d/postgresql
### BEGIN INIT INFO
# Provides: PostgreSQL
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: DB server
# Description: PostgreSQL DB server
### END INIT INFO
prefix=/usr/local/pgsql
PGDATA="$prefix/data"
PGCTL="$prefix/bin/pg_ctl"
case $1 in
start)
echo "PostgreSQL start ..."
${PGCTL} -s -D ${PGDATA} start -w -t 120
;;
stop)
echo "PostgreSQL stop ..."
${PGCTL} -s -D ${PGDATA} stop -m fast
;;
restart)
echo "PostgreSQL restart ..."
${PGCTL} -s -D ${PGDATA} stop -m fast
${PGCTL} -s -D ${PGDATA} start -w -t 120
;;
status)
${PGCTL} -D ${PGDATA} status"
;;
*)
echo "Usage: $0 {start|stop|restart|status}" 1>&2
exit 1
;;
esac
exit 0
・パーミッション変更
[root]# chmod 755 /etc/init.d/postgresql
・リンクを作成 / 削除
[root]# update-rc.d postgresql defaults / remove
・確認
[root]# ls -l /etc/rc*.d | grep postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 K01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 K01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 20 2月 11 19:58 K01postgresql -> ../init.d/postgresql
※ 上記リンクが無くても、Systemd によるサービス管理はできる
[postgres]$ killall postgres
・ユニットファイル記述
デフォルト設定(dnf, rpm アップグレード時に書き換わる)
[root]# vim /usr/lib/systemd/system/postgresql.service
[Unit]
Description = PostgreSQL - Database Server
After = network.target
[Service]
Type = forking
User = postgres
PIDFile = /usr/local/pgsql/data/postmaster.pid
Environment=PGDATA=/usr/local/pgsql/data
ExecStart = /usr/local/pgsql/bin/pg_ctl -s -D ${PGDATA} start -w -t 120
ExecStop = /usr/local/pgsql/bin/pg_ctl -s -D ${PGDATA} stop -m fast
Restart = on-failure
RestartSec = 10s
PrivateDevices = true
PrivateMounts = true
PrivateUsers = true
RestrictNamespaces=true
ProtectControlGroups = true
ProtectKernelModules = true
ProtectKernelTunables = true
ProtectKernelLogs = true
ProtectClock = true
ProtectHome = true
ReadWritePaths = /usr/local/pgsql
ProtectSystem = true
ProtectHostname = true
NoNewPrivileges = true
ProtectProc = noaccess
RestrictSUIDSGID = true
SystemCallArchitectures = native
SystemCallFilter=~@debug
SystemCallFilter=~@swap
SystemCallFilter=~@clock
SystemCallFilter=~@module
SystemCallFilter=~@raw-io
SystemCallFilter=~@reboot
SystemCallFilter=~@obsolete
SystemCallFilter=~@cpu-emulation
RestrictRealtime = true
LockPersonality = true
RemoveIPC = true
PrivateIPC = true
PrivateNetwork = true
MemoryDenyWriteExecute = true
ProcSubset = pid
UMask = 0077
[Install]
WantedBy = multi-user.target
ユーザー設定(優先される、設定変更はこちらに対して行う)
[root]# cp -v /usr/lib/systemd/system/postgresql.service /etc/systemd/system
・ユニットファイル登録/変更時
[root]# systemctl daemon-reload
・起動/停止
[root]# systemctl { start stop restart reload-or-restart } postgresql
・自動起動/停止
[root]# systemctl { enable disable } postgresql
・起動/自動起動を同時に行う
[root]# systemctl enable --now postgresql
・停止/自動停止を同時に行う
[root]# systemctl disable --now postgresql
[user]$ psql -U ユーザー名 -h ホスト名 or IPアドレス -p ポート番号 データベース名
・Unix ドメインソケット経由(/tmp/.s.PGSQL.5432)
[user]$ psql -U ユーザー名 -h /tmp -p 5432 データベース名
[user]$ psql -U ユーザー名 データベース名
・Perl の場合 Unix ドメインソケット経由(/tmp/.s.PGSQL.5432)
my $dbh = DBI->connect (
'DBI:Pg:dbname=DB名;host=/tmp;port=5432;',
'ユーザー名', 'パスワード',
{RaiseError => 1, PrintError => 0, AutoCommit => 1},
);