problem connecting cloud run to postgresql 11 database

Viewed 792

I'm trying to connect my Cloud run container to my PostgreSQL 11 database using this connection string

postgresql+psycopg2://username:password@/db?host=/cloudsql/project-name:region:dbinstance

when I try to run my container it will give back this error

2019-10-24 13:30:53.910 CEST sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused

2019-10-24 13:30:53.910 CEST Is the server running locally and accepting

2019-10-24 13:30:53.910 CEST connections on Unix domain socket "/cloudsql/project-name:region:dbinstance/.s.PGSQL.5432"?

2019-10-24 13:30:53.910 CEST(Background on this error at: http://sqlalche.me/e/e3q8)

I've also tried to use this connection string

postgresql+psycopg2://username:password@/db?unix_socket=/cloudsql/project-name:region:dbinstance

that returned this error message

2019-10-24 11:11:43.733 CESTsqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "unix_socket"

2019-10-24 11:11:43.733 CEST(Background on this error at: http://sqlalche.me/e/f405)

2019-10-24 11:11:44.469 CESTContainer called exit(1).
1 Answers

For connecting to a Cloud SQL instance from Cloud Run, Google has this guide. You can find the Python snippet in there.

As the documentation states, the PostgreSQL standard requires the Unix socket to have a .s.PGSQL.5432 suffix in the socket path. Some libraries apply this suffix automatically, but others require you to specify the socket path as follows:

Related