我得到了错误:

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

知道我哪里错了吗?


当前回答

我在克隆服务器上移动数据目录,登录postgres时遇到了麻烦。像这样重置postgres密码对我有用。

root# su postgres
postgres$ psql -U postgres
psql (9.3.6) 
Type "help" for help. 
postgres=#\password 
Enter new password: 
Enter it again: 
postgres=#

其他回答

大多数解决方案建议编辑pg_hba.conf。

对于不想编辑配置文件的人,基本上只需要登录到postgres用户。如果您在Linux服务器上使用/,则使用此命令

sudo -i -u postgres

它将创建用户postgres,然后登录到它。现在再次尝试psql命令。

你也可以用下面的命令为postgres用户添加密码(你应该是root用户)

passwd postgres

这是可行的,因为根据PostgreSQL的文档,

对等的身份验证

对等体认证的工作原理是获取客户端的认证信息 来自内核的操作系统用户名,并使用它作为允许的 数据库用户名(可选用户名映射)。这个方法是 仅支持本地连接。

问题仍然是你的pg_hba.conf文件*。

这条线:

local   all             postgres                                peer

应该是:

local   all             postgres                                md5

修改此文件后,不要忘记重新启动PostgreSQL服务器。如果你在Linux上,那将是sudo systemctl restart postgresql(在旧系统上:sudo service postgresql restart)。


定位hba.conf

注意,这个文件的位置不是很一致。

您可以使用locate pg_hba.conf或询问PostgreSQL SHOW hba_file;来发现文件位置。

通常位于/etc/postgresql/[version]/main/pg_hba.conf和/var/lib/pgsql/data/pg_hba.conf。


下面是根据官方PostgreSQL文档中的认证方法对peer和md5选项的简要描述。

对等的身份验证

对等体认证的工作原理是获取客户端的认证信息 来自内核的操作系统用户名,并使用它作为允许的 数据库用户名(可选用户名映射)。这个方法是 仅支持本地连接。

密码身份验证

The password-based authentication methods are md5 and password. These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively. If you are at all concerned about password "sniffing" attacks then md5 is preferred. Plain password should always be avoided if possible. However, md5 cannot be used with the db_user_namespace feature. If the connection is protected by SSL encryption then password can be used safely (though SSL certificate authentication might be a better choice if one is depending on using SSL).

我也有同样的问题。

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

然后做出改变。

请按照以下步骤操作

1).首先,进入/etc/postgresql/{你的pg版本}/main目录。

我的版本是10

cd /etc/postgresql/10/main

2).这里驻留pg_hba.conf文件需要在这里做一些更改,你可能需要 Sudo访问此。

sudo nano pg_hba.conf

3).向下滚动文件,直到你找到这个-

# Database administrative login by Unix domain socket
local   all             postgres                                peer

4).此处修改peer为md5,如下所示。

# Database administrative login by Unix domain socket
local   all             all                                md5

peer表示它信任UNIX用户的真实性,因此不信任 提示输入密码。Md5意味着它总是会要求输入密码, 并在MD5散列后进行验证。

5).现在保存文件并重新启动Postgres服务器。

sudo service postgresql restart

现在应该没问题了。

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

#TYPE  DATABASE        USER            ADDRESS                 METHOD
local    all             all                                     trust

并重新加载postgres服务器。

# service postgresql-9.5 reload

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