Error connecting Django to Google CloudSQL Postgres database in Google App Engine
Asked Answered
L

2

7

I'm evaluating using Google Cloud and Google App Engine for our company's new product. I'm trying to adapt this tutorial to use Postgres instead of MySQL:

https://cloud.google.com/python/django/flexible-environment

While I'm able to successfully connect to the database locally, when I try in production, I get the following 500 error:

OperationalError at /admin/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_name_hidden>:us-central1:<database_id_hidden>/.s.PGSQL.5432"?

To connect to Postgres, I made three changes to the sample project. I have this snippet in app.yaml:

beta_settings:
  cloud_sql_instances: <project_name_hidden>:us-central1:<database_id_hidden>

I have this snippet in settings.py:

# [START dbconfig]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'polls',
        'USER': '<db_user_name_hidden>',
        'PASSWORD': '<db_password_hidden>',
        'PORT': '5432',
    }
}
# In the flexible environment, you connect to CloudSQL using a unix socket.
# Locally, you can use the CloudSQL proxy to proxy a localhost connection
# to the instance
DATABASES['default']['HOST'] = '/cloudsql/<project_name_hidden>:us-central1:<database_id_hidden>'
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = '127.0.0.1'
# [END dbconfig]

and have this requirements.py:

Django==1.10.6
#mysqlclient==1.3.10
psycopg2==2.7.1
wheel==0.29.0
gunicorn==19.7.0
Lunation answered 16/3, 2017 at 14:28 Comment(2)
getting the same error. Haven't found any solution. I have created an issue on github: github.com/GoogleCloudPlatform/python-docs-samples/issues/870Reverberation
I am also getting the same error ....Day
L
7

Never mind, seems like Google fixed something on their end and the service is working now. I'm trying to figure out what exactly changed...

Lunation answered 24/3, 2017 at 4:42 Comment(0)
W
0

I battled with this for hours and the only way to fix it was to create a new instance of Postgres. Brute force unfortunately, but it seems that sometimes instances on GCP can start off in a corrupt state and will never work.

Wounded answered 5/8, 2018 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.