I'm experimenting with Cloud SQL for the first time. I'm trying to connect an application running in Cloud Run to a Cloud SQL PostgreSQL database via a private IP. Here's how I have things set up:
However, I get this exception when the application starts and attempts to connect to the database:
org.postgresql.util.PSQLException: FATAL: database "<REDACTED>" does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2676)
at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2788)
at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:174)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:290)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
at org.postgresql.Driver.makeConnection(Driver.java:402)
at org.postgresql.Driver.connect(Driver.java:261)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
The documentation at this page talks about only needing to configure the Cloud Run application to use a Serverless VPC Connector, and then in the code using the Cloud SQL instance's private IP and port to create the connection. But that's what I'm doing and it's not working.
I've also seen this documentation, which talks about how to configure private service access for a Cloud SQL database with a private IP. My database is configured to use an IP range that I believe was created at the time that I first set up the database, although I don't remember for sure. When I look at my VPC network in the console, I see an allocated IP range and a service private connection that uses that IP range. I definitely didn't set those up manually, so my guess is they were auto-created when I was first creating my database and configured it to use an automatically assigned IP range.
I've read some things that suggest that maybe the database private IP needs to fall within the IP range of my Serverless VPC Connecter, but I have no idea if that's true, and if it is, I don't know how to set that up, as I get an error whenever I try to create a Serverless VPC Connector with an IP range that overlaps an existing service private connection, or vice versa.
I'm also using the Google Cloud SQL JDBC Socket Factory library so that I can leverage IAM authentication when connecting to the database. Not sure if that might be factoring into this at all.
Solved! Go to Solution.
Wow, I am a colossal idiot. (In fairness, I was 98.3% certain that the ultimate solution to this was going to be something dumb.)
While I created a database instance, I failed to create an actual database by going to the Databases tab and clicking Create Database. 😖
Have you enabled private IP in your Cloud SQL Socket Factory config?
Thanks for the reply! And yes, here's a sampling of the relevant connection properties I'm setting:
config.setJdbcUrl("jdbc:postgresql://${dbConfig.instanceHost}:${dbConfig.instancePort}/${dbConfig.dbName}")
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory")
config.addDataSourceProperty("cloudSqlInstance", dbConfig.instanceConnectionName)
config.addDataSourceProperty("ipTypes", "PRIVATE")
config.addDataSourceProperty("enableIamAuth", "true")
config.addDataSourceProperty("user", dbConfig.dbIamUser)
// Password is not used but must be set to a nonempty value to bypass driver validation errors.
config.addDataSourceProperty("password", "password")
config.addDataSourceProperty("sslmode", "disable")
Although the documentation you linked to mentions that "If not specified, the default used is ipTypes=PUBLIC,PRIVATE", which should also work, since that means it "will use the instance's Public IP if it exists, otherwise private". In my case, there is no public IP.
Wow, I am a colossal idiot. (In fairness, I was 98.3% certain that the ultimate solution to this was going to be something dumb.)
While I created a database instance, I failed to create an actual database by going to the Databases tab and clicking Create Database. 😖
Ha. No worries. Glad you figured out the issue.