我得到了错误:

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

其他回答

进入/etc/postgresql/9。打开pg_hba.conf文件

在我的例子中:

$>  sudo nano /etc/postgresql/9.3/main/pg_hba.conf

用md5代替peer

所以这将被更改为:

通过Unix域套接字登录数据库 本地所有postgres peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

这样的:

通过Unix域套接字登录数据库 本地所有postgres md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

然后重启pg服务器: $> sudo service postgresql restart

下面是用于连接postgres的方法列表:

# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.

注意:如果你还没有创建postgres用户。创建它,现在您可以使用该用户凭证访问postgres服务器。

提示:如果postgres重启后不工作,请关闭终端并重新打开。

如果你想保持默认配置,但想对一个特定的用户/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

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

享受吧!

下面的命令适用于我:

psql -d myDb -U username -W

许多其他答案与各种配置文件中的设置有关,与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路径设置都应该是正确的!