通过 psycopg2 连接到 CloudSql

Connecting to CloudSql via psycopg2

我正在尝试使用 pycopg2 连接到 GCP 上的 CloudSQL

为了做到这一点,我使用了主机:

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

如"Instance connection name"

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

但是这会导致:

无法将主机名 "my-project-dev-a24525:europe-west1:sql-dev-432521ec-master" 转换为地址:名称解析暂时失败

如果我通过public ip 地址连接,如

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

连接正常。

我在哪里可以找到 GCP 中的正确主机名?

您的云 SQL 实例没有关联的 DNS 名称(除非您给它一个)。

您列出的 "instance connection name" 可用于连接 Cloud SQL Proxy or when interacting with the Cloud SQL Admin API,但没有任何 DNS 意义。