Het is een beetje lastig, maar hier is wat voor mij werkte. Ik zal je helpen met het opzetten van de Quickstart App Engine met psycopg2 en daarna krijg je het idee.
Gebruik de Quickstart voor Python in de flexibele App Engine-omgeving documentatie om uw app in te stellen en te implementeren.
Gebruik de Verbinding maken vanuit App Engine documentatie om verbinding te maken met uw App Engine-app met de Cloud SQL Postgre SQL.
Ik heb kleine wijzigingen aangebracht om dat te laten werken:
In app.yaml toevoegen:
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".
In requirements.txt toevoegen:
psycopg2
psycopg2-binary
In main.py toevoegen:
@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"
Gebruik de gcloud app deploy opdracht om uw app te implementeren.
Gebruik na de implementatie de gcloud app browse commando om de app in de browser te openen.
Bij toegang tot de link https://[PROJECT_ID].appspot.com/connect Het zou moeten reageren met Connection was established!