Я могу получить соединение, работающее со следующей конфигурацией:
PROJECT=[[YOUR-PROJECT-ID]]
REGION=europe-west3
INSTANCE=instance-01
и:
import os
from flask import Flask
import psycopg2
db_user = os.environ.get('CLOUD_SQL_USERNAME')
db_pass = os.environ.get('CLOUD_SQL_PASSWORD')
db_name = os.environ.get('CLOUD_SQL_DATABASE')
db_conn = os.environ.get('CLOUD_SQL_INSTANCE')
app = Flask(__name__)
@app.route('/')
def main():
host = '/cloudsql/{}'.format(db_conn)
cnx = psycopg2.connect(
dbname=db_name,
user=db_user,
password=db_pass,
host=host
)
with cnx.cursor() as cursor:
cursor.execute('SELECT NOW() as now;')
result = cursor.fetchall()
current_time = result[0][0]
cnx.commit()
cnx.close()
return str(current_time)
и:
flask==1.0.2
psycopg2==2.8
и, с${VARIABLE}
заменено значением:
runtime: python37
env_variables:
CLOUD_SQL_INSTANCE: "${PROJECT}:${REGION}:${INSTANCE}"
CLOUD_SQL_USERNAME: ${USERNAME}
CLOUD_SQL_PASSWORD: ${PASSWORD}
CLOUD_SQL_DATABASE: ${DATABASE}