Python 连接到 PostgreSql Redshift 数据库,尝试 运行 许可 sequel 但无法。如何授予权限或运行权限查询?

Python connected to PostgreSql Redshift database, trying to run permission sequel but unable to. How can I grant permission or run permission query?

我正在尝试 运行 使用 Python 使用 psycopg2 连接到 PostgreSql 的查询。我能够连接但无法 运行 提供以 'grant select on ...'

开头的权限的查询

我已经尝试过定期 'select * from column limit 1' 来查看是否建立了连接以及我是否能够进行定期查询。我试过 'grant select on view to user' 但没有用。

permissions = "grant select on view_activities to username;"

try:
    cur.execute(permissions)
    contents = "The permissions have been run"
    message = twilioCli.messages.create(body = contents, from_=twil, to = me)
except:
    contents = "The permissions have failed"
    message = twilioCli.messages.create(body = contents, from_=twil, to = me)

预期结果是收到一条说明 'The permissions have been run' 的短信,这表明此查询 运行 没有问题。但是,我得到的是第二个结果 'The permissions have failed',这表明查询没有正确 运行。我的理论是 postgres 无法 运行 Python 的 'grant select...' 语法。

您的代码的某些部分(可能未在此处添加)似乎正在执行 cursor.fetchall()cursor.fetchone(),这为我提供了相同的异常 psycopg2.ProgrammingError: no results to fetch 。删除 .fetchall().fetchone() 后,我的代码执行时没有任何问题。

此外,确保运行 connection.commit()执行完GRANT,否则会回滚