使用psycopg2连接到Google Cloud SQL Postgres实例 [英] Connecting to Google Cloud SQL Postgres instance using psycopg2
问题描述
我正在尝试使用psycopg2连接到Google Cloud SQL Postgres.
I am trying to connect to Google Cloud SQL Postgres Using psycopg2.
我已经创建了一个postgreSQL实例,并且现在使用默认数据库 postgres
.我可以通过pgadmin工具以及gcloud shell进行连接,并且查询可以提供预期的结果.
I have created an postgreSQL instance and using the default database postgres
for now.
I am able to connect from pgadmin tool as well as from the gcloud shell and the queries give expected result.
我已经开发了Flask应用程序,并在标准应用程序引擎上进行了部署.
I have developed a flask application and deploying on standard app engine.
conn = psycopg2.connect(数据库="postgres",用户="postgres",密码=密码",主机="/cloudsql/my-new-db")
当我运行它时,get psycopg2.OperationalError:无法连接到服务器:没有这样的文件或目录
错误.
And when I run it, the get psycopg2.OperationalError: could not connect to server: No such file or directory
error.
我预感主机值不正确.我尝试了各种选项,例如/cloudsql/< prj-name>.< region>.< db-instance-name>
I have a hunch that host value is not correct. I tried various options like /cloudsql/<prj-name>.<region>.<db-instance-name>
但是,似乎没有任何作用.我还应该怎么做才能消除此错误?
But, nothing seems to be working. What else should I be doing to remove this error?
推荐答案
As mentioned in this article on connecting to Cloud SQL from app engine:
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]
这篇关于使用psycopg2连接到Google Cloud SQL Postgres实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!