简体   繁体   English

连接数据库时出错

[英]Got an error while connecting to database

I am unable to connected to database in linux using psycopg2. 我无法使用psycopg2连接到Linux中的数据库。 I have postgresql installed in linux and my code is: 我在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()   

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 . 该代码在windows7运行良好,但在linux中出现了上述错误。 Is there anything I need to do more in linux? 我需要在Linux上做更多的事情吗? Any suggestion will be appreciated. 任何建议将不胜感激。 Thank you. 谢谢。

It's not a problem due to your code, but due to the permission of the postgres user. 这不是由于您的代码而引起的问题,而是由于postgres用户的许可。

You should create a new user to access to your database and replace this : 您应该创建一个新用户来访问您的数据库并替换为:

conn = psycopg2.connect("dbname = myDatabase user = postgres port = 5432")

by (replace <your_user> by your real user name ...) 通过(用您的真实用户名替换<your_user>您的用户<your_user> ...)

conn = psycopg2.connect("dbname = myDatabase user = <your_user> port = 5432")

To create a new user on postgres, you can use the 'createuser' binary. 要在postgres上创建新用户,可以使用'createuser'二进制文件。 The documentation is available here . 该文档可在此处获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM