CentOS 8
Sponsored Link

PostgreSQL 12 : PostgreSQL over SSL/TLS2020/02/13

 
PostgreSQL で SSL/TLS による暗号化通信の設定を有効にします。
[1]
こちらを参考に SSL 証明書を取得しておきます
当例では、SSL 証明書はリンク先の例のように [/etc/letsencrypt/live/www.srv.world] 配下に取得しているとし、[Common Name] には [www.srv.world] を設定しているものとして進めます。
[2] 作成した証明書をコピーして SSL/TLS の設定をします。
[root@www ~]#
cp /etc/letsencrypt/live/www.srv.world/* /var/lib/pgsql/data/

[root@www ~]#
chown postgres. /var/lib/pgsql/data/*.pem

[root@www ~]#
chmod 600 /var/lib/pgsql/data/*.pem

[root@www ~]#
vi /var/lib/pgsql/data/postgresql.conf
# 100行目:コメント解除して変更

ssl =
on
# 101行目:コメント解除して取得した証明書に変更

ssl_ca_file = '
/var/lib/pgsql/data/chain.pem
'
ssl_cert_file = '
/var/lib/pgsql/data/cert.pem
'
#ssl_crl_file = ''
ssl_key_file = '
/var/lib/pgsql/data/privkey.pem
'
[root@www ~]#
vi /var/lib/pgsql/data/pg_hba.conf
# 77行目以降でアクセス元や認証方式の設定

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
# 最終行に追記
# [hostssl] ⇒ SSL/TLS 使用時のみ TCP/IP ネットワークを使用する
# [10.0.0.0/24] ⇒ アクセス許可するネットワーク
# [md5] ⇒ MD5 パスワード認証を使用する
hostssl all             all             10.0.0.0/24             md5

[root@www ~]#
systemctl restart postgresql
[3] 設定が完了したら、PostgreSQL でアクセス許可を設定したネットワーク内の任意のホストから接続確認をしておきます。
# Unix ドメインソケット接続は通常通り

[cent@www ~]$
psql testdb

psql (12.1)
Type "help" for help.

testdb=> \q

# 宛先ホストを指定した TCP/IP 接続は設定通り SSL/TLS 接続

# SSL/TLS 接続時は以下のように [SSL connection ***] と表示される

[cent@www ~]$
psql -h www.srv.world testdb

Password:
psql (12.1)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

testdb=> \q


# 他ホストからの TCP/IP 接続も SSL/TLS 接続

[root@node01 ~]#
psql -h www.srv.world -d testdb -U cent

Password for user cent:
psql (12.1)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

testdb=>
関連コンテンツ