当前位置: 首页 > 知识库问答 >
问题:

无法连接到PostgreSQL server:致命:用户“postgres”的对等身份验证失败

臧梓
2023-03-14

如何从管理员处访问postgres数据库?

我已更改用户的密码postgres

$ sudo -u postgres psql
$ postgres=# alter user postgres password 'secret';

结果:

ALTER ROLE

但我在管理员身上仍然会出现以下错误:

Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user "postgres"

你知道为什么吗?

我有两个用户:

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 root      | Superuser, Create role, Create DB                          | {}

但是,当我使用以下命令创建用户root时,我没有为其设置密码:

sudo -u postgres createuser --interactive

输出(为什么不要求密码??):

Enter name of role to add: root
Shall the new role be a superuser? (y/n) y

但我仍然得到管理员的错误:

Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user "root"

编辑:

这是我的pg_hba的一些内容。形态

sudo nano /etc/postgresql/9.5/main/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

我将peer更改为ident

# Database administrative login by Unix domain socket
local   all             postgres                                ident

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

然后重启了我的机器。但还是没有运气。

共有2个答案

郎嘉树
2023-03-14

看起来您正在使用“对等身份验证”,而您想要使用“密码身份验证”。使用默认pg_hba.conf,您可以通过指定主机名或“127.0.0.1”切换到密码身份验证。

有关身份验证方法,请参阅Postgres留档。

令狐灿
2023-03-14

我从这里得到了答案:

local   all             postgres                                md5

然后重启服务:

sudo systemctl restart postgresql.service
 类似资料: