1) 生成SSL证书
[root@srv1 ~]# cd /etc/pki/tls/certs
[root@node1 certs]# openssl genrsa -aes128 2048 > postgres.key
Generating RSA private key, 2048 bit long modulus (2 primes)
......+++++
.................+++++
e is 65537 (0x010001)
Enter pass phrase: # 设定密码
Verifying - Enter pass phrase:
[root@node1 certs]# openssl rsa -in postgres.key -out postgres.key
Enter pass phrase for postgres.key: # 输入密码,完成脱密
writing RSA key
[root@node1 certs]# openssl req -utf8 -new -key postgres.key -out postgres.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:1000cc.net
Organizational Unit Name (eg, section) []:tech
Common Name (eg, your name or your server's hostname) []:node1.1000cc.net
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@node1 certs]# openssl x509 -in postgres.csr -out postgres.crt -req -signkey postgres.key -days 3650
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=1000cc.net/OU=tech/CN=node1.1000cc.net
Getting Private key
2) 将所生成的key及crt复制到PostgreSQL目录
[root@srv1 certs]# cp postgres.* /var/opt/rh/rh-postgresql12/lib/pgsql/data/
[root@srv1 certs]# chown postgres /var/opt/rh/rh-postgresql12/lib/pgsql/data/postgres.*
[root@srv1 certs]# chmod 600 /var/opt/rh/rh-postgresql12/lib/pgsql/data/postgres.*
[root@srv1 certs]# cd
[root@srv1 ~]#
3) 配置PostgreSQL以支持SSL
[root@srv1 ~]# vim /var/opt/rh/rh-postgresql12/lib/pgsql/data/postgresql.conf
......
......
# 取消100行注释,并开启ssl
ssl = on
#ssl_ca_file = ''
# 取消102行注释,并指定SSL证书文件
ssl_cert_file = '/var/opt/rh/rh-postgresql12/lib/pgsql/data/postgres.crt'
#ssl_crl_file = ''
# 取消104行注释,并指定SSL Key文件
ssl_key_file = '/var/opt/rh/rh-postgresql12/lib/pgsql/data/postgres.key'
......
......
[root@srv1 ~]# vim /var/opt/rh/rh-postgresql12/lib/pgsql/data/pg_hba.conf
......
......
# 于文件最底部追加如下内容
hostssl all all 192.168.10.0/24 md5
[root@srv1 ~]# systemctl restart rh-postgresql12-postgresql
4) 测试
[root@srv1 ~]# su - snow
[snow@srv1 ~]$ psql -h srv1.1000y.cloud testdb
Password for user snow:
psql (12.1)
# 当看到TLS....字样时即意味着Postgre SQL已支持SSL
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
testdb=> \q
[snow@srv1 ~]$
|