官网SSL说明:https://www.postgresql.org/docs/9.1/libpq-ssl.html
使用SSL需要的4个文件,名称要一致:
File | Contents | Effect |
---|---|---|
~/.postgresql/postgresql.crt | client certificate | requested by server |
~/.postgresql/postgresql.key | client private key | proves client certificate sent by owner; does not indicate certificate owner is trustworthy |
~/.postgresql/root.crt | trusted certificate authorities | checks that server certificate is signed by a trusted certificate authority |
~/.postgresql/root.crl | certificates revoked by certificate authorities | server certificate must not be on this list |
%APPDATA%\postgresql\实例:C:\Users\Administrator\AppData\Roaming\postgresql
测试:
在使用 Spring Boot 连接 PostgreSQL 数据库时,如果需要使用 SSL 连接,那么私钥(sslkey)应该是 PKCS8 格式。可以使用 OpenSSL 工具将私钥转换为这种格式。来自StackOverflow的解决方案原文https://stackoverflow.com/questions/54257758/spring-boot-connection-to-postgresql-with-ssl。以下是具体的命令:
openssl pkcs8 -topk8 -inform PEM -outform DER -in postgresql.key -out postgresql.pk8 -nocrypt
未使用默认路径的自定义配置:
driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:25432/dbname?ssl=true&sslrootcert=pathTo/root.crt&sslcert=pathTo/postgresql.crt&sslkey=pathTo/postgresql.pk8 username: 'username' password: 'password'
使用默认路径的配置:
driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:25432/dbname?sslmode=require username: 'username' password: 'password'
使用默认路径配置时:
# 目录下没有文件时 [FATAL: connection requires a valid client certificate] # 添加root.crt后 [FATAL: connection requires a valid client certificate] # 添加postgresql.crt后 [SSL error: Received fatal alert: unexpected_message] # 添加postgresql.pk8 # 正常访问
# 目录下没有文件时 [Could not open SSL root certificate file C:\Users\Administrator\AppData\Roaming\postgresql\root.crt.] # 添加root.crt后 [FATAL: connection requires a valid client certificate] # 添加postgresql.crt后 [SSL error: Received fatal alert: unexpected_message] # 添加postgresql.pk8 # 正常访问
# 文件权限错误导致的连接失败 # 数据库连接失败:Could not open SSL root certificate file ~./postgresql/root.crt.