如何使用Google App Engine PostgreSQL数据库设置psycopg2 [英] How to setup psycopg2 with Google App Engine PostgreSQL database

查看:100
本文介绍了如何使用Google App Engine PostgreSQL数据库设置psycopg2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在Google App Engine上运行的应用程序,我希望它使用关联的PostgreSQL数据库.我正在使用psycopg2来帮助我进行SQL查询.但是,我不确定如何设置连接.这是我目前拥有的:

I have an application that is being run on Google's App Engine and I want it to use the associated PostgreSQL database. I'm using psycopg2 to help me with my SQL queries. However I am not sure how I can set up the connection. This is what I currently have:

con = psycopg2.connect(
    host=HOST_NAME, # the IP address of the SQL database
    database=DATABASE_NAME, # the name of the database (I'm using the default, so this is "postgres"
    user=USER_NAME, # just the user name that I created
    password=PASSWORD # the password associated to that user
)

但是,当我尝试发出请求时,创建此连接时出现错误psycopg2.OperationalError: could not connect to server: Connection timed out.有什么我想念的吗?

However, when I try to make a request, I get the error psycopg2.OperationalError: could not connect to server: Connection timed out when creating this connection. Is there something I am missing?

推荐答案

有点棘手,但这对我有用.我将帮助您使用psycopg2设置Quickstart App Engine,然后您会明白的.

It is a little bit tricky, but here is what worked for me. I will help you to set up the Quickstart App Engine with psycopg2 and after that you will get the idea.

在App Engine灵活环境中使用 Python快速入门文档来设置和部署您的应用.

Use the Quickstart for Python in the App Engine Flexible Environment documentation to set up and deploy your app.

使用从App Engine连接文档进行连接到您的App Engine应用程序,再到Cloud SQL Postgre SQL.

Use the Connecting from App Engine documentation to connect to your App Engine app to the Cloud SQL Postgre SQL.

为了进行这项工作,我做了一些修改:

I have did slightly modifications in order to make that work:

app.yaml中添加:

beta_settings:
  cloud_sql_instances: [INSTANCE_CONNECTION_NAME]=tcp:5432
#[INSTANCE_CONNECTION_NAME] = [PROJECT_NAME]:[INSTANCE_ZONE]:[INSTANCE_NAME]
#[INSTANCE_CONNECTION_NAME] can be found at Google Cloud Console Cloud SQL's instance page, under "Instance connection name".

requirements.txt中添加:

psycopg2
psycopg2-binary

main.py中添加:

@app.route('/connect')
def connect():
    try:
        #host='172.17.0.1' is the defult IP for the docker container that it is being created during the deployment of the App Engine
        conn = psycopg2.connect("dbname='postgres' user='postgres' host='172.17.0.1' password='test'")
        return "Connection was established!"
    except:
        return "I am unable to connect to the database"

使用gcloud app deploy命令部署您的应用.

Use the gcloud app deploy command to deploy your app.

部署后,使用gcloud app browse命令在浏览器中打开应用.

After the deployment, use the gcloud app browse command to open the app in the browser.

访问链接时https://[PROJECT_ID].appspot.com/connect 它应该以Connection was established!

When accessing the link https://[PROJECT_ID].appspot.com/connect It should respond with Connection was established!

这篇关于如何使用Google App Engine PostgreSQL数据库设置psycopg2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆