使用 psycopg2 连接到 Google Cloud SQL Postgres 实例
Connecting to Google Cloud SQL Postgres instance using psycopg2
我正在尝试使用 psycopg2 连接到 Google 云 SQL Postgres。
我已经创建了一个 postgreSQL 实例并暂时使用默认数据库 postgres
。
我能够从 pgadmin 工具以及 gcloud shell 进行连接,并且查询给出了预期的结果。
我开发了一个 Flask 应用程序并部署在标准应用引擎上。
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = "/cloudsql/my-new-db")
当我 运行 它时,得到 psycopg2.OperationalError: could not connect to server: No such file or directory
错误。
我预感主机值不正确。我尝试了各种选项,例如 /cloudsql/<prj-name>.<region>.<db-instance-name>
但是,似乎没有任何效果。我还应该做些什么来消除这个错误?
如 this article 从 App Engine 连接到云 SQL 中所述:
Connecting with Unix sockets
Once correctly configured, you can connect your service to your Cloud SQL instance's Unix domain socket accessed on the environment's filesystem at the following path: /cloudsql/INSTANCE_CONNECTION_NAME.
The INSTANCE_CONNECTION_NAME can be found on the Overview page for your instance in the Google Cloud Console or by running the following command:
gcloud sql instances describe [INSTANCE_NAME]
在你的 flask 服务器上确保安装 psycopg2 库(pip 安装或使用 apt-get 安装)。我附上了一小段代码,用于成功连接到我的 Postgres 数据库,也许它会以某种方式提供帮助。我注意到您没有指定端口。
connection = psycopg2.connect(
user = 'userName',
password = password,
host = 'some ip here',
port = '5432',
database = 'db name here'
)
在我的例子中,主机是服务器的 IP,它转发了一个端口以访问我的烧瓶服务器。我不确定您访问主机的方式。我的烧瓶 API 和数据库实际上托管在 google 云中的单独 VM 上。我希望这对您有所帮助,如果没有,也许可以提供更多的上下文,我可以多看看它。
unix_socket = '/cloudsql/{}'.format("my-project-id:us-central1:my-db-name")
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = unix_socket)
这对我有用。
我正在尝试使用 psycopg2 连接到 Google 云 SQL Postgres。
我已经创建了一个 postgreSQL 实例并暂时使用默认数据库 postgres
。
我能够从 pgadmin 工具以及 gcloud shell 进行连接,并且查询给出了预期的结果。
我开发了一个 Flask 应用程序并部署在标准应用引擎上。
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = "/cloudsql/my-new-db")
当我 运行 它时,得到 psycopg2.OperationalError: could not connect to server: No such file or directory
错误。
我预感主机值不正确。我尝试了各种选项,例如 /cloudsql/<prj-name>.<region>.<db-instance-name>
但是,似乎没有任何效果。我还应该做些什么来消除这个错误?
如 this article 从 App Engine 连接到云 SQL 中所述:
Connecting with Unix sockets
Once correctly configured, you can connect your service to your Cloud SQL instance's Unix domain socket accessed on the environment's filesystem at the following path: /cloudsql/INSTANCE_CONNECTION_NAME.
The INSTANCE_CONNECTION_NAME can be found on the Overview page for your instance in the Google Cloud Console or by running the following command:
gcloud sql instances describe [INSTANCE_NAME]
在你的 flask 服务器上确保安装 psycopg2 库(pip 安装或使用 apt-get 安装)。我附上了一小段代码,用于成功连接到我的 Postgres 数据库,也许它会以某种方式提供帮助。我注意到您没有指定端口。
connection = psycopg2.connect(
user = 'userName',
password = password,
host = 'some ip here',
port = '5432',
database = 'db name here'
)
在我的例子中,主机是服务器的 IP,它转发了一个端口以访问我的烧瓶服务器。我不确定您访问主机的方式。我的烧瓶 API 和数据库实际上托管在 google 云中的单独 VM 上。我希望这对您有所帮助,如果没有,也许可以提供更多的上下文,我可以多看看它。
unix_socket = '/cloudsql/{}'.format("my-project-id:us-central1:my-db-name")
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = unix_socket)
这对我有用。