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

lau*_*kok 6 postgresql adminer

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

我已经更改了用户的密码postgres

$ sudo -u postgres psql
$ postgres=# alter user postgres password 'secret';
Run Code Online (Sandbox Code Playgroud)

结果:

ALTER ROLE
Run Code Online (Sandbox Code Playgroud)

但是我仍然在管理员上收到此错误:

Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user "postgres"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

任何想法为什么?

我有这两个用户:

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                          | {}
Run Code Online (Sandbox Code Playgroud)

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

sudo -u postgres createuser --interactive
Run Code Online (Sandbox Code Playgroud)

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

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

但我仍然在 adminer 上收到错误消息:

Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user "root"
Run Code Online (Sandbox Code Playgroud)

编辑:

这是我的一些 pg_hba.conf

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
Run Code Online (Sandbox Code Playgroud)

我改peerident

# 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
Run Code Online (Sandbox Code Playgroud)

然后重新启动了我的机器。但仍然没有运气。

lau*_*kok 7

这里得到我的答案:

local   all             postgres                                md5
Run Code Online (Sandbox Code Playgroud)

然后重启服务:

sudo systemctl restart postgresql.service
Run Code Online (Sandbox Code Playgroud)