psql 无法登录,而 pgAdmin 却让我成功登录?命令错误?

psql won't login, while pgAdmin logs me in successfully? Wrong command?

我有一台远程机器,我正在尝试使用以下命令登录到 postgres 数据库(运行 作为 Linux root 用户通过 ssh ,即在那台机器上本地):

# psql -d mydatabase -U postgres -W
Password for user postgres: <here goes my password>
psql: FATAL:  Peer authentication failed for user "postgres"

出于某种原因失败了(尝试了 ~5 次)。

但是当我使用 pgAdmin(即远程)以相同的角色 (postgres) 和相同的密码登录同一个数据库时,它每次都登录成功。

我使用的 psql 命令有什么问题?如果正确,您能否建议任何故障排除步骤?

编辑:所以问题是在 pg_hba.conf 中远程和本地连接的处理方式不同。远程配置正确。问题是为什么本地人不工作。

对于本地人,我在 pg_hba.conf:

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

我是否理解正确,对于 localhost 连接,它首先尝试对等身份验证,如果失败,它不会尝试 md5,只是 returns 错误?

psql 默认尝试通过对等登录。这意味着,您必须在本地计算机上以 postgres 身份登录。你可以强制psql使用TCP/IP,你需要tpo指定一个主机,可以是localhost,所以调用看起来像:

psql -U postgres -h 127.0.0.1

另一种方法是调整您的设置。我看到两种更改当前本地设置的方法:

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

1) 通过设置

行允许所有用户从本地主机无密码登录
local   all             all                                     trust

这是非常危险的,恕我直言,不应该这样做。

2) 另一种方法是创建一个与您的系统登录具有相同用户名的数据库用户,并授予该帐户所有权限

当然,在这种情况下我通常会这样做:运行 psql 在 postgres 用户的上下文中,例如通过 sudo -u postgres psql 或 su -u postgres -c "psql ..."