Ayush Agarwalla
Ayush Agarwalla

Reputation: 30

Django app on Google App Engine not connecting to Google Cloud SQL

I've encountered an issue when deploying my Django application on Google App Engine and trying to connect it to Google Cloud SQL. The application works perfectly on my local system, but when deployed, I get the following connection error:

could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

This is the code snippet for my settings.py file:

DATABASES = {"default": env.db()}


# If the flag as been set, configure to use proxy
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
    DATABASES["default"]["HOST"] = "127.0.0.1"
    DATABASES["default"]["PORT"] = 5432

# [END gaestd_py_django_database_config]
# [END db_setup]

# Use a in-memory sqlite3 database when testing in CI systems
# TODO(glasnt) CHECK IF THIS IS REQUIRED because we're setting a val above
if os.getenv("TRAMPOLINE_CI", None):
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
        }
    }

I have followed this guide for setting up my application on google app engine.

These are all the troubleshooting steps I have tried so far -

psycopg2.OperationalError: could not connect to server: Connection refused

at .connect ( /layers/google.python.pip/pip/lib/python3.9/site-packages/psycopg2/init.py:122 )at .get_new_connection ( /layers/google.python.pip/pip/lib/python3.9/site-packages/django/db/backends/postgresql/base.py:215 )at .inner ( /layers/google.python.pip/pip/lib/python3.9/site-packages/django/utils/asyncio.py:26 )at .connect ( /layers/google.python.pip/pip/lib/python3.9/site-packages/django/db/backends/base/base.py:263 )at .inner ( /layers/google.python.pip/pip/lib/python3.9/site-packages/django/utils/asyncio.py:26 )at .ensure_connection ( /layers/google.python.pip/pip/lib/python3.9/site-packages/django/db/backends/base/base.py:282 )

I also checked the Networking tab for the cloud SQL and it said "App Engine authorisation : All apps in this project are authorised by default."

Upvotes: 1

Views: 504

Answers (1)

Jack Wotherspoon
Jack Wotherspoon

Reputation: 1989

It looks like you may have found a bug in the underlying code.

The Cloud SQL Proxy can be deployed two ways, using a TCP connection (as the quickstart demonstrates) over your localhost 127.0.0.1 through port 5432 (for Postgres, 3306 for MySQL etc.).

This is what the code is expected and why your deployment works locally:

# If the flag as been set, configure to use proxy
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
    DATABASES["default"]["HOST"] = "127.0.0.1"
    DATABASES["default"]["PORT"] = 5432

The Cloud SQL Proxy can also be deployed using a Unix socket instead of a TCP connection. App Engine automatically deploys the Cloud SQL Proxy using a Unix Socket and NOT through TCP. It expects your host to be the path to the Unix socket /cloudsql/PROJECT_ID:REGION:INSTANCE_NAME

It seems like the creator of this code sample/quickstart forgot this detail. I believe if you unset the USE_CLOUD_SQL_AUTH_PROXY environment variable the sample should work when deployed to App Engine as the default is for the code to look for the proper Unix socket from your .env file created in an earlier step.

echo DATABASE_URL=postgres://DATABASE_USERNAME:DATABASE_PASSWORD@//cloudsql/PROJECT_ID:REGION:INSTANCE_NAME/DATABASE_NAME > .env

Which should be set as the default as long as the USE_CLOUD_SQL_AUTH_PROXY is unset or set to None

DATABASES = {"default": env.db()}

I'll file a bug to get this code sample updated.

Upvotes: 2

Related Questions