我得到了错误:

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

知道我哪里错了吗?


当前回答

我也有同样的问题。

depa的解决方案是绝对正确的。

只要确保你有一个用户配置使用PostgreSQL。

检查文件:

$ ls /etc/postgresql/9.1/main/pg_hba.conf -l

这个文件的权限应该给你注册psql的用户。

进一步。如果你到现在还好…

按照@depa的指示更新。

i.e.

$ sudo nano /etc/postgresql/9.1/main/pg_hba.conf

然后做出改变。

其他回答

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

奏效的方法是:

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

具有sudo权限的Vi编辑器。

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

#TYPE  DATABASE        USER            ADDRESS                 METHOD
local    all             all                                     trust

并重新加载postgres服务器。

# service postgresql-9.5 reload

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

如果您有问题,您需要找到pg_hba.conf。命令如下:

查找/ -name 'pg_hba.conf' 2>/dev/null

然后修改配置文件:

Postgresql 9.3

Postgresql 9.4

下一步是:重新启动你的db实例:

Service postgresql-9.3 restart

如果您有任何问题,您需要重新设置密码:

修改用户密码为db_password

如果你想保持默认配置,但想对一个特定的用户/db连接进行socket md5认证,在“local all/all”行之前添加一个“local”行:

# TYPE  DATABASE     USER         ADDRESS             METHOD

# "local" is for Unix domain socket connections only
local   dbname       username                         md5  # <-- this line
local   all          all                              peer
# IPv4 local connections:
host    all          all          127.0.0.1/32        ident
# IPv6 local connections:
host    all          all          ::1/128             ident

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

host: localhost

整个文件如下所示

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

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