CentOS7のpostgreSQL自動起動設定~外部接続まで
CentOS7にpostgreSQLをソースからコンパイルでインストールの続き
PostgreSQLをインストールしたディレクトリ
/home/mayser/mypgsql
起動スクリプトを配置するディレクトリとファイル名
/usr/lib/systemd/system/postgresql.service
起動時のパス設定
# 2019/09 postgresqlのパス通す
export PATH=/home/mayser/pgsql/bin:$PATH
起動スクリプトの準備
/usr/lib/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=mayser
# Group=admin
Environment=PGDATA=/home/mayser/mypgsql
Environment=PGLOG=/home/mayser/mypgsql/serverlog
OOMScoreAdjust=-1000
ExecStart=/home/mayser/pgsql/bin/pg_ctl -D /home/mayser/mypgsql start
ExecStop=/home/mayser/pgsql/bin/pg_ctl -D /home/mayser/mypgsql stop
TimeoutSec=300
[Install]
WantedBy=multi-user.target
その後、以下のコマンドにて起動
(OS再起動後もpostgreSQL起動される)
sudo systemctl enable postgresql
systemctl start postgresql
systemctl list-unit-files | grep "postgres"
外部からの接続許可設定
postgresqlはデフォルトでは自ホストからしか繋げないようになっています。その為postgresqlにクライアントアプリからアクセスするためには設定が必要になります。
以下の設定が必要になります。
1.CentOS上のF/W許可
2./home/mayser/mypgsql/postgresql.confの編集
3./home/mayser/mypgsql/pg_hba.confの編集
CentOS上のF/W許可
postgresql.confの編集
# ------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
# ------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;←コメントを解除し、''内を * に修正
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)←コメントを解除し、必要あればポート番号を変更する
max_connections = 100 # (change requires restart)
pg_hba.confの編集
全てのホストからの接続を許可する
全てのホストから、全てのデータベースへの接続を許可する場合、以下の記述を追加する。
host all all 0.0.0.0/0 trust
接続確認
まずは、telnetレベルでのネットワーク疎通確認(5432ポートが開いているか?)
telnet 192.168.10.8 5432
真っ黒な画面が出たら接続されています。
DBへの接続は、psqledit を使います。