我得到了错误:

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

知道我哪里错了吗?


当前回答

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

host: localhost

整个文件如下所示

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

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

其他回答

安装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基于密码的认证

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

请按照以下步骤操作

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

现在应该没问题了。

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

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

sudo -i -u postgres

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

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

passwd postgres

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

对等的身份验证

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

许多其他答案与各种配置文件中的设置有关,与pg_hba.conf有关的答案确实适用,并且是100%正确的。但是,请确保您正在修改正确的配置文件。

正如其他人所提到的,配置文件的位置可以在主配置文件中使用各种设置来覆盖,也可以在命令行上使用-D选项提供主配置文件的路径。

您可以在psql会话中使用以下命令来显示正在读取配置文件的位置(假设您可以启动psql)。这只是一个故障排除步骤,可以帮助一些人:

select * from pg_settings where setting~'pgsql';  

您还应该确保postgres用户的主目录位于您预期的位置。我这样说是因为很容易忽略这一点,因为您的提示符将显示'~'而不是您的主目录的实际路径,这使得它不那么明显。许多安装默认postgres用户主目录为/var/lib/pgsql.

如果它没有被设置为它应该是什么,停止postgresql服务,并在以root身份登录时使用以下命令。同时确保postgres用户没有登录到另一个会话:

usermod -d /path/pgsql postgres

最后,通过输入echo $PGDATA来确保你的PGDATA变量设置正确,它应该输出类似于:

/path/pgsql/data

如果它没有设置,或者显示的内容与您期望的不同,请检查您的启动文件或RC文件,如.profile或.bash。rc -这将根据您的操作系统和shell而有很大的不同。一旦你为你的机器确定了正确的启动脚本,你可以插入以下内容:

export PGDATA=/path/pgsql/data

对于我的系统,我把它放在/etc/profile.d/profile.local.sh中,这样所有用户都可以访问它。

现在您应该能够像往常一样初始化数据库,所有的psql路径设置都应该是正确的!

如果不提供主机,可能会出现此错误。下面的场景与此类似。

user@homepc:~$ psql -d test_db -U test_user psql: error: FATAL: Peer authentication failed for user" test_user" user@homepc:~$ psql -h localhost -d test_db -U test_user test_user用户密码:

提供主机解决了我的问题在psql命令行。尝试在rails中为postgress提供主机连接配置。