Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django in Google Cloud Run cant find /cloudsql socket

I have a dockerized django application which I've built, uploaded to GCR and then deployed as a google cloud run service. However, when starting up I get the following error (from the cloud run logs):

psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running locally and accepting 
connections on Unix domain socket "/cloudsql/kingdoms-289503:us-west1:kingdomsdb/.s.PGSQL.5432"?

My database settings for django look something like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': "/cloudsql/kingdoms-289503:us-west1:kingdomsdb",
        'USER': "postgres",
        'PASSWORD': "password",
        'NAME': "postgres",
    }
}

And I've made sure to create the connection to the database

cloudsql connection

From what I understand, cloud run is supposed to magically mount a at /cloudsql that django can use to connect to postgres, but the error implies that it's not being mounted.

Is there an extra piece of configuration I would need to check to make sure that socket is there? Are there alternatives to connecting to cloudsql that don't involve this socket?

like image 503
Forrest Keppler Avatar asked Aug 31 '25 05:08

Forrest Keppler


1 Answers

As mentioned above by @glasnt, it could also be an ACL issue. The service account needs the Cloud SQL Client role. In the default case, Default compute service account is the principal ending in [email protected].

My issue was that I wasn't given the option to add the role from where my newbie intuition led me, the Cloud Run service resource. Instead, go to the main IAM settings, IAM tab, and manually add the permission to the service account. If you don't see the service account in the list and thus can't edit it's roles, you can go to Add + (top toolbar) and manually enter the email address.

like image 132
Aly Ibrahim Avatar answered Sep 02 '25 18:09

Aly Ibrahim