PostgreSQL(起動/停止)

起動/停止(RedHat : SysVinit, Upstart)

    [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 }

起動/停止(Ubuntu : SysVinit, Upstart)

    [postgres]$ killall postgres

Sysvinit の init プロセス用リンクを作成、削除。サービスをソースインストールした場合、設定が必要。


・/etc/init.d にスクリプトを設置
    [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 によるサービス管理はできる

起動/停止(RedHat, Ubuntu : 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
Environment=SYSTEMD_LOG_LEVEL=debug,console:info
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
KillMode = control-group
Restart = on-failure
RestartSec = 10s

SystemCallArchitectures = native
SystemCallFilter=~@clock
SystemCallFilter=~@cpu-emulation
SystemCallFilter=~@debug
SystemCallFilter=~@module
SystemCallFilter=~@mount
SystemCallFilter=~@obsolete
SystemCallFilter=~@privileged
SystemCallFilter=~@raw-io
SystemCallFilter=~@reboot
SystemCallFilter=~@resources
SystemCallFilter=~@swap

RestrictRealtime = yes

RemoveIPC = yes
PrivateIPC = yes

CapabilityBoundingSet =

RestrictAddressFamilies =~ AF_PACKET AF_NETLINK

User = postgres
Group = postgres
PermissionsStartOnly = yes
UMask = 0077

ProtectSystem = full
ProtectHome = yes
ReadWritePaths = /usr/local/pgsql
PrivateUsers = yes

PrivateDevices = yes
ProtectClock = yes
ProtectKernelLogs = yes
ProtectKernelModules = yes
ProtectKernelTunables = yes
ProtectControlGroups = yes

PrivateNetwork = yes
NoNewPrivileges = yes
IPAddressAllow = localhost
IPAddressAllow = 192.168.1.0/24
IPAddressDeny = any
MemoryDenyWriteExecute = yes
ProcSubset = pid

RestrictNamespaces = yes
RestrictSUIDSGID = yes
ProtectHostname = yes
LockPersonality = yes
ProtectProc = invisible

[Install]
WantedBy = multi-user.target

ユーザー設定(優先される、設定変更はこちらに対して行う)

    [root]# cp -v /usr/lib/systemd/system/postgresql.service /etc/systemd/system
・ユニットファイル登録/変更時
    [root]# systemctl daemon-reload
・systemd によるセキュリティスコア
    [root]# systemd-analyze security postgresql.service

    → Overall exposure level for postgresql.service: 1.0 OK
・起動/停止
    [root]# systemctl { start stop restart reload-or-restart } postgresql.service
・自動起動/停止
    [root]# systemctl { enable disable } postgresql.service
・起動/自動起動を同時に行う
    [root]# systemctl enable --now postgresql.service
・停止/自動停止を同時に行う
    [root]# systemctl disable --now postgresql.service

データベースへアクセス

・ネットワーク経由
    [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},
                        );