【发布时间】:2019-02-19 11:48:06
【问题描述】:
我无法将 Google Cloud PostgreSQL 与 JDBC 连接。
Java 版本:1.8.0_162
PostgreSQL JDBC 版本:42.2.2
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
}
String url = "jdbc:postgresql://ipaddresshere:5432/postgres";
Properties props = new Properties();
props.setProperty("user","postgres");
props.setProperty("password","passwordhere");
props.setProperty("ssl","true");
props.setProperty("sslcert","/Users/bguler/Desktop/GoogleCloudPGSSL/client-cert.pem");
props.setProperty("sslkey","/Users/bguler/Desktop/GoogleCloudPGSSL/client-key.pem");
props.setProperty("sslrootcert","/Users/bguler/Desktop/GoogleCloudPGSSL/server-ca.pem");
try {
Connection conn = DriverManager.getConnection(url, props);
System.out.println(conn);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
我测试的证书路径是对的。
它抛出一个错误为 ;
原因:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
SSL 错误:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
我可以毫无问题地通过 psql 在终端上连接;
psql "sslrootcert=server-ca.pem \
sslcert=client-cert.pem sslkey=client-key.pem \
hostaddr=ipaddress \
port=5432 \
user=postgres dbname=postgres"
【问题讨论】:
标签: java postgresql ssl google-cloud-sql