将Google Cloud函数连接到Cloud sql(postgreSQL) [英] connecting google cloud function to cloud sql (postgreSQL)

查看:47
本文介绍了将Google Cloud函数连接到Cloud sql(postgreSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将google cloud功能与google cloud sql(postgreSQL)连接起来.

我找到了文档,并且对文档执行了完全相同的操作(我认为...),但是我遇到了错误.

此后一切都按预期进行.

I'm trying to connect google cloud function with google cloud sql (postgreSQL).

I found document and do exactly same things with document (I think...) but I got error.

https://cloud.google.com/sql/docs/postgres/quickstart-connect-functions

my cloud function requirements.txt

SQLAlchemy==1.3.12
pg8000==1.13.2

and my 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'

And I got error like this

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)

Why error said MissingColumn, did I use wrong query? Please help me.. How can I solve this problem..

解决方案

I followed the tutorial and faced the same issue.

I had to change the line of code:

stmt = sqlalchemy.text('INSERT INTO entries (guestName, content) values ("third guest", "Also this one");')

to

stmt = sqlalchemy.text("INSERT INTO entries (guestName, content) values ('third guest', 'Also this one');")

Everything worked as expected after that.

这篇关于将Google Cloud函数连接到Cloud sql(postgreSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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