我得到了错误:

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时也不会显示。

知道我哪里错了吗?


当前回答

您只需要将METHOD设置为信任。

#TYPE  DATABASE        USER            ADDRESS                 METHOD
local    all             all                                     trust

并重新加载postgres服务器。

# service postgresql-9.5 reload

修改pg_hba.conf不需要重启postgres服务器。重新加载。

其他回答

如果您通过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

享受吧!

如果你在使用rails时遇到了这个问题,并且你知道你已经创建了用户名和密码以及正确的权限,那么你只需要在你的数据库末尾添加以下内容。yml文件。

host: localhost

整个文件如下所示

development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: root
  password: admin
  host: localhost

你根本不需要修改pg_hba.conf文件。快乐的编码

上面的编辑对我来说是有效的,因为我发现在做了这些编辑之后我需要重新启动postgres服务器。 ubuntu的:

sudo /etc/init.d/postgresql restart

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

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

sudo -i -u postgres

psql

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