将谷歌云功能连接到云 sql (postgreSQL)
Posted
技术标签:
【中文标题】将谷歌云功能连接到云 sql (postgreSQL)【英文标题】:connecting google cloud function to cloud sql (postgreSQL) 【发布时间】:2021-01-25 15:42:03 【问题描述】:我正在尝试将 google cloud 功能与 google cloud sql (postgreSQL) 连接起来。
我找到了文档并且对文档做了完全相同的事情(我认为...)但是我得到了错误。
https://cloud.google.com/sql/docs/postgres/quickstart-connect-functions
我的云功能需求.txt
SQLAlchemy==1.3.12
pg8000==1.13.2
还有我的 main.py
import sqlalchemy
# Set the following variables depending on your specific
# connection name and root password from the earlier steps:
connection_name = "tram-2020:us-central1:tram"
db_password = "[my password]"
db_name = "guestbook"
db_user = "postgres"
driver_name = 'postgres+pg8000'
query_string = dict("unix_sock": "/cloudsql//.s.PGSQL.5432".format(connection_name))
def insert(request):
request_json = request.get_json()
stmt = sqlalchemy.text('INSERT INTO entries (guestName, content) values ("third guest", "Also this one");')
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername=driver_name,
username=db_user,
password=db_password,
database=db_name,
query=query_string,
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)
try:
with db.connect() as conn:
conn.execute(stmt)
except Exception as e:
return 'Error: '.format(str(e))
return 'ok'
我遇到了这样的错误
Error: (pg8000.core.ProgrammingError) 'S': 'ERROR', 'V': 'ERROR', 'C': '42703', 'M': 'column "third guest" does not exist', 'P': '50', 'F': 'parse_relation.c', 'L': '3359', 'R': 'errorMissingColumn'
[SQL: INSERT INTO entries (guestName, content) values ("third guest", "Also this one");]
(Background on this error at: http://sqlalche.me/e/f405)
为什么错误说 MissingColumn,是我使用了错误的查询吗? 请帮助我..我该如何解决这个问题..
【问题讨论】:
【参考方案1】:我按照教程操作并遇到了同样的问题。
我不得不更改代码行:
stmt = sqlalchemy.text('INSERT INTO entries (guestName, content) values ("third guest", "Also this one");')
到
stmt = sqlalchemy.text("INSERT INTO entries (guestName, content) values ('third guest', 'Also this one');")
之后一切都按预期进行。
【讨论】:
以上是关于将谷歌云功能连接到云 sql (postgreSQL)的主要内容,如果未能解决你的问题,请参考以下文章
我们可以将谷歌云 SQL 与 Amazon Elastic Beanstalk 一起使用吗
如何在谷歌云平台中使用私有 IP 从我的电脑连接到 sql 实例