我得到了错误:

FATAL: Peer authentication failed for user "postgres"

当我尝试让postgres与Rails一起工作时。

这是pg_hba.conf,我的数据库。Yml,以及完整跟踪的转储。

我在pg_hba中将身份验证更改为md5,并尝试了不同的方法,但似乎都不起作用。

我还尝试按照Rails 3.2创建一个新用户和数据库,FATAL: Peer authentication failed for user (PG::Error)

但是它们不会在pgadmin上显示,甚至当我运行sudo -u postgres psql -l时也不会显示。

知道我哪里错了吗?


当前回答

在我的情况下,我甚至无法编辑或查看pg_hba.conf文件的内容。

奏效的方法是:

/etc/postgresql/14/main$ sudo vi pg_hba.conf

具有sudo权限的Vi编辑器。

其他回答

修改pg_hba.conf中的METHOD peer为trust (/etc/postgresql/9.1/main/pg_hba.conf |第85行)可以解决这个问题。添加md5需要密码,因此如果需要避免使用密码,请使用trust而不是md5。

这对我来说很有效!!

sudo -u postgres psql

最简单的解决方案,无需更改配置。(ubuntu) 修改用户,然后连接数据库cli。

sudo -i -u postgres

psql

摘自https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04

另外,如果您没有从postgres帐户访问脚本的权限,您可以使用下面的方法。

$ cat ./init-user-db.sh | sudo -i -u postgres bash

如果您通过localhost(127.0.0.1)连接,则不应该遇到这个特定问题。我不会对pg_hba.conf做太多的改动,但我会调整你的连接字符串:

psql -U someuser -h 127.0.0.1 database

其中someuser是你连接的用户,database是你的用户有权限连接的数据库。

下面是我在Debian上设置postgres的方法:

http://www.postgresql.org/download/linux/debian/  (Wheezy 7.x)

as root …

    root@www0:~# echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list

    root@www0:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

    root@www0:~# apt-get update

    root@www0:~# apt-get install postgresql-9.4        

    root@www0:~# su - postgres 

    postgres@www0:~$ createuser --interactive -P someuser
    Enter password for new role:
    Enter it again:
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) y
    Shall the new role be allowed to create more new roles? (y/n) n

    postgres@www0:~$ createdb -O someuser database

    postgres@www0:~$ psql -U someuser -h 127.0.0.1 database

享受吧!