Search code examples
psycopg2google-cloud-sql

Connecting to CloudSql via psycopg2


I am trying to connect to CloudSQL on GCP using pycopg2

In order to do this I use as host:

'my-project-dev-a24525:europe-west1:sql-dev-432521ec-master'

as given in "Instance connection name"

conn = psycopg2.connect(host='my-project-dev-a24525:europe-west1:sql-dev-432521ec-master',
database="postgres", user="admin", password="password")

However this results in:

could not translate host name "my-project-dev-a24525:europe-west1:sql-dev-432521ec-master" to address: Temporary failure in name resolution

If I connect via the public ip address as in

conn = psycopg2.connect(host='xx.xxx.xx.xxx', database="postgres", user="admin", password="password")

the connection is fine.

Where can I find the proper hostname in GCP?


Solution

  • Your Cloud SQL instance doesn't have a DNS name associated with it (unless you give it one).

    The "instance connection name" which you have listed can be used to connect with the Cloud SQL Proxy or when interacting with the Cloud SQL Admin API, but doesn't have any DNS significance.