我得到了错误:

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帐户访问脚本的权限,您可以使用下面的方法。

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

其他回答

这对我来说很有效!!

sudo -u postgres psql

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

sudo /etc/init.d/postgresql restart

安装Postgresql后,我执行了以下步骤。

Open the file pg_hba.conf. For Ubuntu, use for example /etc/postgresql/13/main$ sudo nano pg_hba.conf and change this line at the bottom of the file, it should be the first line of the settings: local all postgres peer to local all postgres trust Side note: If you want to be able to connect with other users as well, you also need to change: local all all peer to local all all md5 If you used nano editor, exit with double Escape, x, y, Enter to save the config file. Restart the server $ sudo service postgresql restart Output: * Restarting PostgreSQL 13 database server Login into psql and set your password $ psql -U postgres db> ALTER USER postgres with password 'your-pass'; Output: ALTER ROLE Side note: If you have other users, they will need a password as well: db> ALTER USER my_user with password 'your-pass'; Then enter: exit Finally change the pg_hba.conf from local all postgres trust to local all postgres md5 Restart the server again $ sudo service postgresql restart Output: * Restarting PostgreSQL 13 database server Login at psql with postgres user After restarting the postgresql server, the postgres user accepts the password that you chose: psql -U postgres Output: Password for user postgres: psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1)) Type "help" for help. And you are in psql: postgres=# Side note: Same now works for my_user if you added the user and password: psql -d YOUR_DB_NAME -U my_user Which will ask you for the new password of my_user.

认证方法详细说明:

信任——任何可以连接到服务器的人都被授权访问数据库

对端-使用客户端操作系统用户名作为数据库用户名访问它。

Md5基于密码的认证

如需进一步参考资料,请点击这里

如果您有问题,您需要找到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