0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS7のpostgreSQL自動起動設定~外部接続まで

Posted at

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

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許可

再起動後も有効にする為に、永続設定とする。
image.png

postgresql.confの編集

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の編集

全てのホストからの接続を許可する
全てのホストから、全てのデータベースへの接続を許可する場合、以下の記述を追加する。

pg_hba.conf
host all all 0.0.0.0/0 trust

接続確認

まずは、telnetレベルでのネットワーク疎通確認(5432ポートが開いているか?)

telnet 192.168.10.8 5432 

真っ黒な画面が出たら接続されています。

DBへの接続は、psqledit を使います。
image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?