简体   繁体   中英

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.

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.

Try using the full url in host parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM