无法使用psycopg连接到centos上的postgres

时间:2017-03-02 06:27:57

标签: python postgresql psycopg

我已经在centos上下载了postgres,创建了一个数据库,用密码创建了一个用户,重新启动了postgres,在pg_authid中检查了用户,再次完成了,我仍然无法连接。

为什么我无法连接?

postgres=# create database test;
CREATE DATABASE
postgres=# create user test with password 'test';
CREATE ROLE
postgres=# grant all privileges on database test to test;
GRANT
postgres=# select * from pg_authid where rolname = 'test';
 rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit |             rolpassword             | rolvaliduntil 
---------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------------------------------+---------------
 test    | f        | t          | f             | f           | f            | t           | f              |           -1 | md505a671c66aefea124cc08b76ea6d30bb | 
(1 row)
postgres=# alter user test with password 'test';
ALTER ROLE

在此期间,我已多次sudo service postgresql restart,但仍然是这样:

>>> conn = psycopg2.connect(host="127.0.0.1", port="5432", dbname="test", user="test", password="test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  Ident authentication failed for user "test"

我没有想法:(

编辑:

我也尝试过改变

# "local" is for Unix domain socket connections only
local   all             all                                     peer

# "local" is for Unix domain socket connections only
local   all             all                                     trust

1 个答案:

答案 0 :(得分:1)

必须将pg_hba.conf配置更改为:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the