4

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using the full url in host parameter

4 Comments

Thanks. Can you give an example of the 'full url'
Sure, use some thing like postgres://<hostname>/path, or use an external library for better compability eg: dj-database-url pypi.org/project/dj-database-url
But then, what is the host name going to be? Sorry if I'm asking too many questions.
You need to reffer your cloud provider's settings for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.