Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Cloud Run can't access Google SQL

Denny1
New Member

I have a Google Cloud Run service that needs to access data stored in a Google Cloud SQL instance using PostgreSQL. I've followed the general steps of creating both the Cloud Run service and the Cloud SQL instance, but I'm having trouble establishing the connection between them.

I've tried using the following methods:

  • Connecting directly to the Cloud SQL instance: I've tried connecting to the Cloud SQL instance using the connection string provided in the Cloud SQL instance details, but I'm getting connection errors.
  • Using environment variables: I've tried setting environment variables in my Cloud Run service to provide the connection information, but I'm still unable to connect.
0 2 1,042
2 REPLIES 2

Hi @Denny1,

Welcome to Google Cloud Community!

To connect your Google Cloud Run service to a Google Cloud SQL instance using PostgreSQL, here’s what you can do:

  1. If you're going the public IP route, you have a couple of options. First, you can use the Cloud SQL Auth Proxy. You’d connect through Unix sockets by pointing to the path /cloudsql/INSTANCE_CONNECTION_NAME, where the INSTANCE_CONNECTION_NAME is formatted as project:region:instance-id. You can find this information in the Google Cloud Console or by running gcloud sql instances describe [INSTANCE_NAME]. The cool thing is that these connections are automatically encrypted, so no extra setup is needed. 

    Alternatively, you can use a Cloud SQL connector, which are specific libraries for different programming languages that handle encryption and IAM-based authorization for you.

  2. If you prefer a private IP connection, you can connect directly to the Cloud SQL instance through a VPC network using its private IP address and port 5432.

For more detailed steps and examples, you may refer to this documentation guide on how to connect to Cloud SQL from Cloud Run, or you can check this quickstart for connecting from Cloud Run to Cloud SQL for PostgreSQL that does just that.

I hope the above information is helpful.

Hello. I have the same problem with a MySql cloud instance. I'm trying to connect to it via a fonction in cloud run. The cloud run service has access to the database instance in the service settings. Both proprietary and service accounts have SQL instance access rôle.

I'm using a cloud.sql.connector to initiate the connection, which works ok. But when connecting the resulting pool to the DB it says I dont have the rights to do that. Though I'm connected to the DB with Root account.

I'm testing the service in gcloud shell. The logged user is the proprietary account, not the service one (I dont know why it should make a difference). When testing the cloud SQL via the proxy it works fine.

Really, this is complex as hell to setup. I'm not even at a stage to access the DB from outside that it doesn't even work from inside. I dont understand what's going on. 

If you need anymore info, I can provide. Thanks in advance.