Essay - Published: 2022.06.22 | cloud-run | cloud-sql | database | google-cloud | postgres |
DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)
So you've got a service up and running on Google Cloud Run and a Postgres database instance in CloudSQL. But how do you actually use that database from Cloud Run?
In this post, I'm going to share step-by-step instructions for configuring your Google Cloud Project and Cloud Run service so it can access CloudSQL.
For this post, we're going to assume that you already have:
I have a post going over how to setup a CloudSQL Postgres database for <$10 per month if you still need to do that.
The easiest way to connect Cloud Run and CloudSQL is through the CloudSQL Proxy. This requires your CloudSQL instance to be configured with a Public IP but makes it very simple to connect to your instance with minimal extra configuration required.
Cloud Run <> Cloud SQL Proxy
To allow Cloud Run to connect to CloudSQL via the CloudSQL Proxy:
Cloud RunEdit & Deploy New Revision > ConnectionsAdd Connection under Cloud SQL Connections
Cloud SQL Admin APIOnce the new revision is deployed, you should be able to connect to CloudSQL from Cloud Run!
Now that our permissions / connections are configured, we now need to do the actual connection to CloudSQL from Cloud Run. How you actually create this connection will largely depend on your technologies of choice, but most connections require the same core parameters so we'll list those here.
Example connection parameters
/cloudsql/CONNECTIONNAME
postgrespostgresThe best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.