I can't connect Google Cloud PostgreSQL with JDBC.
Java Version : 1.8.0_162
PostgreSQL JDBC Version : 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());
}
I tested certificate paths are right.
Its throw an error as ;
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
SSL error: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I can connect on terminal via psql without a problem ;
psql "sslrootcert=server-ca.pem \
sslcert=client-cert.pem sslkey=client-key.pem \
hostaddr=ipaddress \
port=5432 \
user=postgres dbname=postgres"