PostgreSQL(起動/停止)

起動/停止(systemd : CentOS7, ScientificLinux7)

    [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
      ProtectSystem = true     # /usr, /boot 読み取りのみ
      ProtectHome = true       # /home, /root, /run/user アクセスできない
      ReadWritePaths = /usr/local/pgsql    # 指定ディレクトリ読み書き許可
      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

      [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

起動/停止(upstart, sysvinit : CentOS6, ScientificLinux6)

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

データベースへアクセス

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