开发者

Got an error while connecting to database

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 e开发者_JAVA百科gg ( 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.


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 :

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

by (replace <your_user> by your real user name ...)

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

To create a new user on postgres, you can use the 'createuser' binary. The documentation is available here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜