我得到了错误:

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

知道我哪里错了吗?


当前回答

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

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

其他回答

进入/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重启后不工作,请关闭终端并重新打开。

上面的编辑对我来说是有效的,因为我发现在做了这些编辑之后我需要重新启动postgres服务器。 ubuntu的:

sudo /etc/init.d/postgresql restart

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

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

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

sudo -i -u postgres

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

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

passwd postgres

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

对等的身份验证

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