Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Unable to connect to Cloud SQL (PostgreSQL) database from Django app in GAE

Hi,

I teach a software development class where students progressively build a web application in Django, and eventually deploy it to Google App Engine. This is the second year we do this sequence of homeworks and, last year, we were able to successfully deploy the apps by essentially following these instructions: https://cloud.google.com/python/django/appengine

When updating the homework this year, we were able to complete all the steps but, when we got to the very end (after setting up the database and deploying the app), we keep getting the following error:

connection to server on socket "/cloudsql/rabble-borja:us-central1:rabble/.s.PGSQL.5432" failed: Connection refused
	Is the server running locally and accepting connections on that socket?

Furthermore, the Cloud SQL logs show the following:

Cloud SQL connection failed. Please see https://cloud.google.com/sql/docs/mysql/connect-app-engine-standard for additional details: config invalidated after TLS handshake failed, error = certificate had CN "", expected "rabble-borja:rabble"

(yes, the error message includes a reference to mySQL, even though it is definitely a PostgreSQL database)

Please note that the app works correctly when we run it locally and use Cloud SQL Auth Proxy to connect to the database. We're able to initialize the database, upload a fixture with some test data, and run the app without any issues. So, the database itself is definitely running, has the correct data and user, etc. The issue seems to be specifically when the app deployed on GAE tries to connect to the database.

We've tried several things to debug this issue:

  • We've run through the instructions in https://cloud.google.com/python/django/appengine to the letter (to make sure it wasn't an issue with some subtle step missing from the instructions we prepared for the students). We still get the exact same error.
  • I asked a TA to run through the instructions (both ours and the official ones) with their account, to make sure this wasn't some configuration issue in my account specifically. They still get the same error.
  • We tried using a MySQL database and get the same error. We also tried deploying a PostgreSQL 15 database (the same one we used last year), and still get the same error.
  • There a few pieces of documentation that mention giving the Cloud SQL Client role to the app account in IAM. We've tried this and still get the same issue.
  • We've double-checked that the app and database are being deployed to the same region (us-central1).

Any suggestions on what could be going on?

1 1 9
1 REPLY 1

I’m experiencing exactly the same issue. I’ve been trying to fix it for hours and still haven’t found a solution 😞