0

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.

1
  • Check this Stackoverflow question explains about 127.17.0.1 default IP Commented Jul 18, 2019 at 13:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Unfortunately this is on Google App Engine I don't think I can change the networking via the Dockerfile.
This is how you can accomplish this when running locally - when running in App Engine Flex the enviroment provides the port on 127.0.0.1 for you. See the Connecting from App Engine page.
I'm seeing this behavior on App Engine Flex environment. I followed the same docs, but it seems to be a bug in App Engine
In your original comment, you mentioned that you are using the proxy. Are you attempting to run the proxy inside the App Engine environment?
That's how google app engine connects to cloud sql. Via cloud sql proxy, at least on flexible environment.
|
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.