且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostgreSQL:致命-用户的对等身份验证失败(PG :: ConnectionBad)

更新时间:2021-12-08 08:33:47

对等身份验证表示它正在使用unix套接字并希望连接的Unix用户具有与PostgreSQL用户名相同的Unix用户名。

"Peer authentication" means that it's using a unix socket and expecting the connecting unix user to have the same unix username as the postgresql username.

由于您本地的Unix用户名是 funkdified ,而您尝试通过用户 goodsounds 通过Unix域套接字( local )连接,而您的 pg_hba.conf 指定 peer 身份验证,Pg正确拒绝了您的连接尝试。

Since your local unix username is funkdified and you're trying to connect as user goodsounds over a unix domain socket (local) connection where your pg_hba.conf specifies peer authentication, Pg correctly rejects your connection attempt.

这是使用unix套接字时许多安装的默认行为。

This is the default behaviour for many installs when using unix sockets.

您可以:


  • 通过在数据库连接设置中指定主机名来通过TCP / IP连接;

  • 编辑 pg_hba.conf 使用 md5 密码身份验证代替 peer 身份验证来使用unix套接字( local 连接类型),因此Pg接受密码身份验证;或

  • 使用与您的Unix用户名相同的PostgreSQL用户名进行连接,并在PostgreSQL用户名中创建该用户(如果尚不存在)。

  • Connect via TCP/IP by specifying a hostname in your database connection settings;
  • edit pg_hba.conf to use md5 password authentication instead of peer authentication for unix sockets (local connection type) so Pg accepts password authentication; or
  • Connect with a PostgreSQL username the same as your unix username and create the user in PostgreSQL if it doesn't exist yet.

请参见 pg_hba.conf 和其余的客户端文档的认证一章

请注意,对 pg_hba.conf 的更改不会立即生效,必须重新启动或至少重新加载PostgreSQL才能重新读取 pg_hba.conf

Note that changes to pg_hba.conf do not take effect immediately, you must restart or at least reload PostgreSQL to get it to reread pg_hba.conf.

同样,如果您安装了多个PostgreSQL版本,则可能有一个版本的libpq和另一版本的服务器。在这种情况下,请确保libpq默认连接到的unix套接字的位置与服务器的 unix_socket_directories 相同,或使用(例如)覆盖它。连接字符串中的host = / tmp

Oh, also, if you have multiple PostgreSQL versions installed you might have a libpq from one version and a server from another. In this case make sure the location for the unix socket that libpq connects to by default is the same as the server's unix_socket_directories or override it with (e.g.) host=/tmp in your connection string.