App engine+Postgresql getting “Connection refused” error

I am a newbie trying to deploy a toy django app on the standard App engine and I am getting the following errors.

Running App locally

My app runs properly locally with the cloud SQL when I use or Public IP as ‘HOST’ address. However, I get a this error if I use GCP connection name like this:

'HOST':  '/cloudsql/asim800:us-central1:django-mysql-1'

OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/asim800:us-central1:django-mysql-1'

Running App on GAE

  1. Using HOST=’/cloudsql/asim800:us-central1:django-mysql-1′, I get this error
    2003, “Can’t connect to MySQL server on ‘localhost’ ([Errno 111] Connection refused)” – not sure why I am getting ‘localhost’ in the error but there seems to be something wrong with translating Google connection name to host and port information.
  2. Using public IP for mysql instance on the GAE results in timeout.

My looks like this

        'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '/cloudsql/asim800:us-central1:django-mysql-1', 
                'NAME': 'myproject',
                'USER': 'pblogsu',
                'PASSWORD': '****',

I can see them correctly in the following log:

{‘database’: ‘myproject’,
‘host’: ‘/cloudsql/asim800:us-central1:django-app1’,
‘password’: ‘pblogsu’,
‘user’: ‘hidden’}

I have enabled Google Cloud SQL API. Not sure how to debug Google connection name mapping to host/port addresses.
I have seen similar problem reported like here ( but none of the suggestions have helped me.
Any help would be greatly appreciated.

PS: I have edited my question substantially. Earlier I had used postgesql with very similar results.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Check the parameters unix_socket_directories and port on the PostgreSQL server. For your connection attempt to work

  • the server has to run on the same machine as the client
  • cloudsql/asim800:us-central1:django-app1 has to be in unix_socket_directories
  • port has to be 5432

Method 2

I was finally able to figure out that one of the service account (associated with GAE) needed to be added in the IAM & Admin> IAM and then given Cloud sql.Client role.

It sounds like an easy fix with the hindsight but it took a lot of digging in to resolve the issue. I had suspected that there may be a permission issue but I wasn’t sure which account needs to be added. I got my first clue after I saw this error in the logs:

"CloudSQL warning: your action is needed to update your application and avoid potential disruptions. Please see for additional details: ensure that the account has access to "asim800:us-central1:django-mysql-1" (and make sure there's no typo in that name). Error during createEphemeral for asim800:us-central1:django-mysql-1: googleapi: Error 403: The client is not authorized to make this request., notAuthorized"

Thanks for your help. I’ll be happy add any details if someone else run into the issue.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x