AdamG
AdamG

Reputation: 2630

Google Cloud Run: Cant connect to Postgress SQL

I am following this tutorial to upload my existing Django project running locally on sqlite to Google Cloud Run / Postgres.

I have the cloud_sql_proxy service running and can sign into Postgres from the command line.

I am at the point of running the command

python manage.py migrate

And I get the error:

django.db.utils.OperationalError: connection to server on socket "/cloudsql/cgps-registration-2:us-central-1:cgps-reg-2-postgre-sql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

The answer to that questions is Yes, the server is running locally and accepting connections because I can log in with the Postgres client:

agerson@agersons-iMac ~ % psql "sslmode=disable dbname=postgres user=postgres hostaddr=127.0.0.1"  
Password for user postgres: 
psql (14.1, server 13.4)
Type "help" for help.

postgres=> 

I double checked the connection string in my .env file and it has the correct UN / P

Is this scoket not getting created somehow in a previous step?

/cloudsql/cgps-registration-2:us-central-1:cgps-reg-2-postgre-sql/.s.PGSQL.5432

Upvotes: 3

Views: 1989

Answers (2)

Jon Peck
Jon Peck

Reputation: 86

Took me a while to stumble across this section of the docs: https://cloud.google.com/python/django/appengine#database_connection

Basically, make sure you have this in your settings.py, after the initial DB config:

# 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

Then before you run locally, be sure to:

export USE_CLOUD_SQL_AUTH_PROXY=true

Upvotes: 0

enocom
enocom

Reputation: 1816

It looks like there's a mismatch between what the app is looking for and how you're launching the proxy. The error explains the problem.

You're launching the proxy like this with an incorrect region name (us-central):

cloud_sql_proxy -instances="cgps-registration-2:us-central:cgps-reg-2-postgre-sql=tcp:5432

But the app is looking for us-central1. Try this (omitting the =tcp:5432 to create a Unix socket):

cloud_sql_proxy -instances="cgps-registration-2:us-central1:cgps-reg-2-postgre-sql

Upvotes: 2

Related Questions