我得到了错误:

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

知道我哪里错了吗?


当前回答

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

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

然后修改配置文件:

Postgresql 9.3

Postgresql 9.4

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

Service postgresql-9.3 restart

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

修改用户密码为db_password

其他回答

请按照以下步骤操作

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。命令如下:

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

然后修改配置文件:

Postgresql 9.3

Postgresql 9.4

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

Service postgresql-9.3 restart

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

修改用户密码为db_password

我在克隆服务器上移动数据目录,登录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_config用于遵从性信息,帮助扩展和客户端程序根据PostgreSQL编译和链接。它对机器上的活动PostgreSQL实例一无所知,只知道二进制文件。

pg_hba.conf可以出现在许多其他位置,这取决于Pg是如何安装的。标准的位置是数据库data_directory中的pg_hbase .conf(可以在/home, /var/lib/pgsql, /var/lib/postgresql/[version]/, /opt/postgres/,等等等等),但是用户和打包者可以把它放在他们喜欢的任何地方。不幸的是。

找到pg_hba.conf的唯一有效方法是询问正在运行的PostgreSQL实例的pg_hba.conf在哪里,或者询问系统管理员它在哪里。你甚至不能依赖于询问datadir在哪里并解析postgresql.conf,因为init脚本在启动Pg时可能会传递一个参数,比如-c hba_file=/some/other/path。

你要做的是问PostgreSQL:

SHOW hba_file;

这个命令必须在超级用户会话上运行,所以对于shell脚本来说,你可以这样写:

psql -t -P format=unaligned -c 'show hba_file';

并设置环境变量“PGUSER”、“PGDATABASE”等,确保连接正确。

是的,这有点像鸡生蛋还是蛋生鸡的问题,因为如果用户无法连接(比如,在搞砸了编辑pg_hba.conf之后),你就无法找到pg_hba.conf来修复它。

另一个选项是查看ps命令的输出,看看postmaster data目录参数-D是否可见。

ps aux  | grep 'postgres *-D'

因为pg_hba.conf将在data目录中(除非你在Debian/Ubuntu或一些衍生版本上使用他们的包)。

如果你的目标是Ubuntu系统,PostgreSQL是从Debian/Ubuntu包中安装的,这就容易一些了。你不需要处理从源文件手工编译的Pg,比如有人在他们的主目录下initdb了一个datadir,或者在/opt目录下安装了一个EnterpriseDB Pg,等等。你可以询问pg_wrapper, Debian/Ubuntu多版本Pg管理器,其中PostgreSQL正在使用pg_wrapper中的pg_lsclusters命令。

If you can't connect (Pg isn't running, or you need to edit pg_hba.conf to connect) you'll have to search the system for pg_hba.conf files. On Mac and Linux something like sudo find / -type f -name pg_hba.conf will do. Then check the PG_VERSION file in the same directory to make sure it's the right PostgreSQL version if you have more than one. (If pg_hba.conf is in /etc/, ignore this, it's the parent directory name instead). If you have more than one data directory for the same PostgreSQL version you'll have to look at database size, check the command line of the running postgres from ps to see if it's data directory -D argument matches where you're editing, etc. https://askubuntu.com/questions/256534/how-do-i-find-the-path-to-pg-hba-conf-from-the-shell/256711

我也有同样的问题。

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

然后做出改变。