Postgres psycopg2创建用户 [英] Postgres psycopg2 create user
本文介绍了Postgres psycopg2创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要通过psycopg2创建用户,我使用以下代码:
To create a user through psycopg2, I am using the following code :
cur=conn.cursor()
cur.execute("create user %s with password %s",('abcdefgh','0h/9warrAttrgd8EF0gkvQ==',))
这将产生以下错误:
syntax error at or near "'abcdefgh'" LINE 1: create user 'abcdefgh' with password '0h/9warrAttrgd8EF0gkvQ.
似乎%s会将引号放在用户名周围,而postgres在创建用户名时不喜欢用户。以下代码可以正常工作:
It seems that %s is placing quotes around the username, which postgres doesn't like while creating a user. The following code works fine :
cur.execute("create user abcdefgh with password %s",('0h/9warrAttrgd8EF0gkvQ==',))
此问题的任何解决方法?
Any workaround for this ?
推荐答案
使用 psycopg2.extensions.AsIs(object):
from psycopg2.extensions import AsIs
cur.execute("create user %s with password %s", (AsIs('abcdefgh'), '0h/9warrAttrgd8EF0gkvQ==',))
这篇关于Postgres psycopg2创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文