我得到了错误:

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

知道我哪里错了吗?


当前回答

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

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提供主机连接配置。

其他回答

修改pg_hba.conf中的METHOD peer为trust (/etc/postgresql/9.1/main/pg_hba.conf |第85行)可以解决这个问题。添加md5需要密码,因此如果需要避免使用密码,请使用trust而不是md5。

如果您通过localhost(127.0.0.1)连接,则不应该遇到这个特定问题。我不会对pg_hba.conf做太多的改动,但我会调整你的连接字符串:

psql -U someuser -h 127.0.0.1 database

其中someuser是你连接的用户,database是你的用户有权限连接的数据库。

下面是我在Debian上设置postgres的方法:

http://www.postgresql.org/download/linux/debian/  (Wheezy 7.x)

as root …

    root@www0:~# echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list

    root@www0:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

    root@www0:~# apt-get update

    root@www0:~# apt-get install postgresql-9.4        

    root@www0:~# su - postgres 

    postgres@www0:~$ createuser --interactive -P someuser
    Enter password for new role:
    Enter it again:
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) y
    Shall the new role be allowed to create more new roles? (y/n) n

    postgres@www0:~$ createdb -O someuser database

    postgres@www0:~$ psql -U someuser -h 127.0.0.1 database

享受吧!

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

安装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文件的内容。

奏效的方法是:

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

具有sudo权限的Vi编辑器。