Connect to Google Cloud SQL from Java


Recently, I migrated my application to Google Cloud Run and Cloud SQL, and I struggled with connecting to the database. So, I’d like to share one way to achieve the connection.

URL and SockeFactory

First, you need to use the correct JDBC URL:


As you can see, you have to provide the name of the database instead of the hostname in the URL, and then the parameter

  • cloudSqlInstance
  • socketFactory
  • user
  • password

Everything can be found in the configuration of your Cloud SQL instance. But what about the socketFactory? This one depends on the DB product, for example, PostgreSQL and requires an additional dependency:


That’s it from the Java side.

Finally, you must configure your Cloud Run service to connect to Cloud SQL. A detailed description can be found here:

Simon Martinelli
Follow Me