-4

I am trying to connect to cloud-sql for a django project in cloud run which is being deployed through cloud run in production. However Django throws me an error saying

Error: 'asia-south1' is not a valid port number.

My database settings in settings.py is as follows

if os.getenv('ENV') == 'PROD':
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'HOST': '/cloudsql/<PROJECT-NAME>:asia-south1:<INSTANCE-ID>',
            'PORT': '5432',
            'USER': <USER>,
            'PASSWORD': <PASSWORD>,
            'NAME': <DB_NAME>,
        }
    }

I saw one possible solution as redirecting cloud proxy connections to 127.0.0.1:3306 but for that I'd have to install cloud proxy on the docker container and authenticate right? Isn't that what cloud run provides us without any of that hassle? Is there anyway of connecting to the cloud sql instance without installing cloud proxy on the container and redirection?

varun krishna
  • 173
  • 1
  • 2
  • 9

1 Answers1

1

How you can see in the documentation here, the Cloud SQL connexion of Cloud Run opens a Unix socket

Once correctly configured, you can connect your service to your Cloud SQL instance's unix domain socket using the format: /cloudsql/INSTANCE_CONNECTION_NAME.

So, your Django configuration is set to open a connexion through TCP and thus look for an IP/DNS and a port.

To connect Django to a Unix socket, you can follow this example/tutorial

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • Thanks for the answer! I did read the answer however i was unable to understand how to connect to unix sockets in host, i have done the same thing as they did in the tutorial prepending the host variable with the a forward slash. However it still throws the same error. Could you throw more light on as to how connect django application to the database via a unix socket? – varun krishna Aug 08 '20 at 14:34
  • 1
    Read Django or library documentation on how to connect using unix sockets. If not working, try using VPC access connector. – ahmet alp balkan Aug 11 '20 at 05:28
  • This does not work with Docker containers deployed with Cloud Run. – David Yerrington Jan 01 '22 at 00:12