Mike
Mike

Reputation: 4259

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?

Upvotes: 0

Views: 1463

Answers (2)

You need to add '/cloudsql/' to your instance connection name, so just replace

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

with

conn = psycopg2.connect(host='/cloudsql/my-project-dev-a24525:europe-west1:sql-dev-432521ec-master',

database="postgres", user="admin", password="password")

Upvotes: 0

kurtisvg
kurtisvg

Reputation: 3565

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.

Upvotes: 1

Related Questions