0
votes

I have a Python 3 application deployed in Google App Engine, flexible environment.

I'm using psycopg2 to connect to a PostgreSQL instance hosted in Google cloud SQL.

I'm having trouble connecting to PostgreSQL from Google App Engine.

Cloud SQL Proxy seems to initialize ok, but it binds to 0.0.0.0

Listening on 0.0.0.0:5432 for projectID:us-central1:my-db

Trying to connect on 127.0.0.1 or localhost doesn't work. Connection is refused.

What does work is using the docker (app engine flexible environment uses docker underneath) default IP 172.17.0.1 (from the docker0 adapter)

Using that IP address to connect to Cloud SQL seems like it would bite me in the ass if someone decides to change it.

Why is this happening?

Is using the default docker0 adapter's IP address a viable long term solution?

Is there an alternative other than switching to a socket based connection instead of the tcp approach.

2
Check this Stackoverflow question explains about 127.17.0.1 default IP - TasosV

2 Answers

1
votes

It sounds like you are running the Cloud SQL proxy on your host machine, while you are attempting to run your application from inside a container. The reason it can't connect to the proxy is because 127.0.0.1 refers to docker's loopback interface, while the proxy is bound to the host machine's interface. The 172.17.0.1 is the address the container can use to can reach the host interface.

One alternative is to use host networking (https://docs.docker.com/network/host/), by passing in --network host. This will cause the host's interface to be used for the application.

-1
votes

I've switched from using TCP as the connection method and to using a Unix Socket.

The TCP issue seems to be a bug in the app engine flexible environment. But it's a beta feature (it is under the name beta_settings in app.yaml) and I'm not holding out for Google to fix it.

I also don't want to commit to an IP address that could be changed sometime in the future as a workaround.