Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user “postgres”
如何从adminer访问postgres数据库?
我更改了用户
1 2 | $ sudo -u postgres psql $ postgres=# ALTER USER postgres password 'secret'; |
结果:
1 | ALTER ROLE |
但我仍然在adminer上得到这个错误:
1 | Unable TO CONNECT TO PostgreSQL server: FATAL: Peer authentication failed FOR USER"postgres" |
有什么想法吗?
我有这两个用户:
1 2 3 4 5 6 | 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 | {} |
但是当我使用此命令创建用户时,我没有为用户
1 | sudo -u postgres createuser --interactive |
输出(为什么不要求密码?):
1 2 | Enter name OF ROLE TO ADD: root Shall the NEW ROLE be a superuser? (y/n) y |
但我仍然在adminer上得到错误:
1 | Unable TO CONNECT TO PostgreSQL server: FATAL: Peer authentication failed FOR USER"root" |
编辑:
这是我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
我将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # 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 |
然后重新启动我的机器。 但仍然没有运气。
从这里得到我的答案:
1 | LOCAL ALL postgres md5 |
然后重启服务:
1 | sudo systemctl restart postgresql.service |
看起来您正在使用"对等身份验证",您希望使用"密码身份验证"。 使用默认的pg_hba.conf,您可以通过指定主机名或"127.0.0.1"切换到密码验证。
有关身份验证方法,请参阅Postgres文档。