Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Django application on Cloud Run using Cloud SQL PostgreSQL DB - cannot connect to database

I have this Django project on Google Cloud on Cloud Run. It is supposed to connect to the Cloud SQL PostgreSQL database and work, but it doesn't. Here is the database part of settings.py (I have removed the private data):

DATABASES = {
  'default': {   
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'NAME': 'db',
      'USER': 'user',
      'PASSWORD': 'password',
      'HOST': '/cloudsql/project:europe-west4:instance',
      'PORT': '5432',
  }
}

However, when I try to do anything database-related (for example, log in), it throws the error

OperationalError at /accounts/login/
could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/*project*:europe-west4:*instance*/.s.PGSQL.5432"?

If I remove the /cloudsql/ from HOST it gives the error

OperationalError at /accounts/login/
could not translate host name "*project*:europe-west4:*instance*" to address: Name or service not known

When I use the Cloud SQL proxy exe to test on my local computer it works. However, when I deploy it to Cloud Run it doesn't.

like image 475
Arca Ege Cengiz Avatar asked Oct 26 '25 18:10

Arca Ege Cengiz


1 Answers

Have you enabled connecting to that Cloud SQL instance from Cloud Run?

Try running

gcloud run services update [your cloud run service name] \
  --add-cloudsql-instances [project:region:instance]

Also check that your Cloud Run service account has Cloud SQL Client permissions. See this documentation for more information on how to do that.

like image 134
Shubha Rajan Avatar answered Oct 28 '25 23:10

Shubha Rajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!