我得到了错误:

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。

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

sudo -i -u postgres

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

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

passwd postgres

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

对等的身份验证

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

其他回答

如果您通过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_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).

在连接中使用host=localhost。

PGconn *conn = PQconnectdb(
    "host=localhost user=postgres dbname=postgres password=123"
);

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

然后做出改变。