mathew
mathew

Reputation: 987

Error connecting Django to Google CloudSQL Postgres database in Google App Engine

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

Upvotes: 7

Views: 2930

Answers (2)

Nicko
Nicko

Reputation: 659

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.

Upvotes: 0

mathew
mathew

Reputation: 987

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...

Upvotes: 7

Related Questions