首页 > 数据库 >二、postgresql 14为数据库配置ssl支持,创建sslinfo扩展

二、postgresql 14为数据库配置ssl支持,创建sslinfo扩展

时间:2022-10-22 19:38:47浏览次数:36  
标签:10 postgresql 14 pg14 server ssl pg root Oct

1、配置SSL

创建自签名证书
openssl req -new -x509 -days 365 -nodes -text -out server.crt -keyout server.key -subj "/CN=cdh01"
chmod og-rwx server.key
创建一个证书签名请求(CSR)和一个公共密钥文件
openssl req -new -nodes -text -out root.csr -keyout root.key -subj "/CN=cdh01"
chmod og-rwx root.key
使用密钥对请求进行签名以创建根证书颁发机构
openssl x509 -req -in root.csr -text -days 3650 -extfile /etc/ssl/openssl.cnf -extensions v3_ca -signkey root.key -out root.crt
创建由新的根证书颁发机构签名的服务器证书
openssl req -new -nodes -text -out server.csr -keyout server.key -subj "/CN=cdh01"
chmod og-rwx server.key

openssl x509 -req -in server.csr -text -days 365 -CA root.crt -CAkey root.key -CAcreateserial -out server.crt
server.crt和server.key应该存储在服务器上,并且root.crt应该存储在客户端上,以便客户端可以验证服务器的叶证书已由其受信任的根证书签名。root.key应该离线存储以用于创建将来的证书。
[root@cdh01 ~]# ls -l
total 32
-rw-------. 1 root root 2023 Jun 6 10:46 anaconda-ks.cfg
-rw-r--r-- 1 root root 1082 Oct 10 16:00 root.crt
-rw-r--r-- 1 root root 3308 Oct 10 15:58 root.csr
-rw------- 1 root root 1708 Oct 10 15:58 root.key
-rw-r--r-- 1 root root 17 Oct 10 16:02 root.srl
-rw-r--r-- 1 root root 964 Oct 10 16:02 server.crt
-rw-r--r-- 1 root root 3308 Oct 10 16:02 server.csr
-rw------- 1 root root 1704 Oct 10 16:02 server.key
[root@cdh01 ~]# cp server.key server.crt /home/pg14/data/ 复制证书和公钥到data数据目录
[root@cdh01 ~]# chown -R pg14.pg14 /home/pg14/
[root@cdh01 ~]# ls -l /home/pg14/data/
total 72
drwx------ 5 pg14 pg14 41 Oct 10 11:19 base
drwx------ 2 pg14 pg14 4096 Oct 10 13:15 global
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_commit_ts
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_dynshmem
-rw------- 1 pg14 pg14 4789 Oct 10 11:19 pg_hba.conf
-rw------- 1 pg14 pg14 1636 Oct 10 11:19 pg_ident.conf
drwx------ 4 pg14 pg14 68 Oct 10 13:19 pg_logical
drwx------ 4 pg14 pg14 36 Oct 10 11:19 pg_multixact
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_notify
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_replslot
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_serial
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_snapshots
drwx------ 2 pg14 pg14 6 Oct 10 13:14 pg_stat
drwx------ 2 pg14 pg14 80 Oct 10 16:14 pg_stat_tmp
drwx------ 2 pg14 pg14 18 Oct 10 11:19 pg_subtrans
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_tblspc
drwx------ 2 pg14 pg14 6 Oct 10 11:19 pg_twophase
-rw------- 1 pg14 pg14 3 Oct 10 11:19 PG_VERSION
drwx------ 3 pg14 pg14 60 Oct 10 11:19 pg_wal
drwx------ 2 pg14 pg14 18 Oct 10 11:19 pg_xact
-rw------- 1 pg14 pg14 88 Oct 10 11:19 postgresql.auto.conf
-rw------- 1 pg14 pg14 28736 Oct 10 16:12 postgresql.conf
-rw------- 1 pg14 pg14 52 Oct 10 13:14 postmaster.opts
-rw------- 1 pg14 pg14 74 Oct 10 13:14 postmaster.pid
-rw-r--r-- 1 pg14 pg14 964 Oct 10 16:14 server.crt
-rw------- 1 pg14 pg14 1704 Oct 10 16:14 server.key

2、启用ssl配置

vi /home/pg14/data/postgresql.conf
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
ssl_prefer_server_ciphers = on
ssl_ecdh_curve = 'prime256v1'
vi /home/pg14/data/postgresql.conf

hostssl all all 0.0.0.0/0 md5


[pg14@cdh01 ~]$ pg_ctl reload -D /home/pg14/data/
server signaled
[pg14@cdh01 ~]$ psql -p5666 -U postgres
psql (14.5)
Type "help" for help.
postgres=# show ssl;
ssl
-----
on
(1 row)
postgres=# show ssl_key_file ;
ssl_key_file
--------------
server.key
(1 row)
postgres=# show ssl_cert_file ;
ssl_cert_file
---------------
server.crt
(1 row)
[pg14@cdh01 ~]$ psql -p5666 -U postgres -h156.25.236.10
Password for user postgres:
psql (14.5)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=#
[pg14@cdh01 ~]$ psql "host='cdh01' user=postgres dbname=postgres password=1qaz@WSX sslmode=require port=5666"
psql (14.5)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

3、创建sslinfo扩展

[pg14@cdh01 sslinfo]cd /home/pg14/postgresql-14.5/contrib/sslinfo

[pg14@cdh01 sslinfo] make && make install

[pg14@cdh01 sslinfo]$ psql -p5666 -U postgres
psql (14.5)
Type "help" for help.

postgres=# create extension sslinfo;
CREATE EXTENSION

标签:10,postgresql,14,pg14,server,ssl,pg,root,Oct
From: https://blog.51cto.com/u_10930585/5786048

相关文章