为什么psycopg2可以连接到我的数据库而我不能

时间:2011-11-20 22:39:43

标签: django postgresql

我正在尝试对我的Django站点进行数据库转储,但Django的本机dumpdata命令对于大型数据库来说相当糟糕,所以我想使用pg_dump。

这有效:

python
>>> import psycopg2
>>> psycopg2.connect(database='blah',user='blah',password='blah',host='localhost')

这不起作用

bash
# psql -U blah blah
psql: FATAL:  Ident authentication failed for user "blah"
# pg_dump -U blah blah
pg_dump: [archiver (db)] connection to database "blah" failed: FATAL:  Ident authentication failed for user "blah"

我的搜索建议我可能需要在/etc/postgresql/9.0/main/pg_hba.conf中将“ident”更改为“password”。这是一个合理的改变吗?为什么我不能使用任何已经适用于python psycopg2的方法进入pg_dump?

1 个答案:

答案 0 :(得分:5)

如果在psql / pg_dump命令行上提供-h localhost,它将通过TCP套接字而不是unix套接字连接。这些可以在pg_hba.conf中定义不同的身份验证方法。由于“ident身份验证”仅适用于unix套接字,因此肯定会出现这种情况。

将pg_hba.conf改为使用“md5”代替“ident”代替“local”行(unix套接字)是合理的,坚持使用postgresql用户帐户中定义的密码而不是期望unix用户名来识别postgresql用户名。

在默认的Debian配置中,用于验证为“postgres”的行分别放入,以便您可以将其他用户的默认值更改为md5或信任,只留下一个帐户受到保护。 Istr从Postgres用户那里听到Debian / Ubuntu默认使用“ident”身份验证更加困惑而不是有用的问题。