连接数据库时出错
我无法使用 psycopg2 连接到 Linux 中的数据库。我在 Linux 中安装了 postgresql,我的代码是:
import psycopg2
def testEgg():
conn = psycopg2.connect("dbname = myDatabase user = postgres port = 5432")
cur = conn.cursor()
cur.execute("CREATE TABLE egg ( num integer, data varchar);")
cur.execute("INSERT INTO egg values ( 1, 'A');")
conn.commit()
cur.execute("SELECT num from egg;")
row = cur.fetchone()
print row
cur.close()
conn.close()
testEgg()
我收到错误:
psycopg2.OperationalError: FATAL: Ident authentication failed for user "postgres"
此代码在 windows7
中运行良好,但在 linux
中出现上述错误。 linux 还有什么需要我做的吗?任何建议将不胜感激。 谢谢。
I am unable to connected to database in linux using psycopg2. I have postgresql installed in linux and my code is:
import psycopg2
def testEgg():
conn = psycopg2.connect("dbname = myDatabase user = postgres port = 5432")
cur = conn.cursor()
cur.execute("CREATE TABLE egg ( num integer, data varchar);")
cur.execute("INSERT INTO egg values ( 1, 'A');")
conn.commit()
cur.execute("SELECT num from egg;")
row = cur.fetchone()
print row
cur.close()
conn.close()
testEgg()
And the I got the error:
psycopg2.OperationalError: FATAL: Ident authentication failed for user "postgres"
This code runs well in windows7
but got above mentioned error in linux
.
Is there anything I need to do more in linux? Any suggestion will be appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是你的代码的问题,而是由于 postgres 用户的许可造成的。
您应该创建一个新用户来访问您的数据库并将其替换
为:(将
替换为您的真实用户名...)要在 postgres 上创建新用户,您可以使用“创建用户”二进制文件。该文档位于此处。
It's not a problem due to your code, but due to the permission of the postgres user.
You should create a new user to access to your database and replace this :
by (replace
<your_user>
by your real user name ...)To create a new user on postgres, you can use the 'createuser' binary. The documentation is available here.