I am trying to connect to my GCP projects PostgreSQL CloudSQL instance from my local machine. The PostgreSQL doesn’t have a public IP, only private.
Properties connProps = new Properties(); connProps.setProperty("user", "XXX-compute@developer.gserviceaccount.com"); connProps.setProperty("password", "password"); connProps.setProperty("sslmode", "disable"); connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory"); connProps.setProperty("cloudSqlInstance", "coral-XXX-XXXX:us-central1:mdm"); connProps.setProperty("enableIamAuth", "true"); HikariConfig config = new HikariConfig(); config.setJdbcUrl(jdbcURL); config.setDataSourceProperties(connProps); config.setConnectionTimeout(10000); // 10s HikariDataSource connectionPool = new HikariDataSource(config);
I get the below error
Failed to get driver instance for jdbcUrl=jdbc:postgresql:///mdm
java.sql.SQLException: No suitable driver
I have verified that my username, instancename, IAM connectivity is all working fine. The IAM service account I am using is my compute engine’s default service account.
Should I be able to connect to this PostgreSQL instance from my local machine?
Advertisement
Answer
First, make sure you’re configuring your JDBC URL correctly.
The URL should look like this:
jdbc:postgresql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>
See the docs for details.
Second, if your Cloud SQL instance is Private IP only, your local machine won’t have a network path to it, unless you’ve explicitly configured one (see this answer for options).
Generally, the simplest way to connect to a private IP instance is to run a VM in the same VPC as the instance, and connect from that VM.