“sudo -u postgres psql postgres”不要求输入密码

Bur*_*ome 1 postgresql logins password debian

我已经接管了一个使用 postgres 的系统。登录时,如果我使用...

sudo -u postgres psql postgres
Run Code Online (Sandbox Code Playgroud)

...它不要求输入密码。但是,如果我这样做...

psql -U postgres -h localhost postgres
Run Code Online (Sandbox Code Playgroud)

...它要求输入密码。

为什么?这种差异是如何配置的?是否可以为不同的用户配置以这种方式登录?

(注意:用户postgres不在sudoers文件中,也不在 sudo 组中。

小智 6

Postgres 有一种称为peer的身份验证类型。如果您查看 pg_hba.conf,您可能会看到类似

local   all             postgres                       peer
Run Code Online (Sandbox Code Playgroud)

或喜欢

local   all             all                            peer
Run Code Online (Sandbox Code Playgroud)

您没有被要求输入密码这一事实与使用 sudo 无关,而与您连接到与用户名匹配的数据库这一事实有关,并且在 pg_hba.conf 中启用了对等身份验证。

如果这是一台测试机器并且不需要安全性,您可以使用信任而不是对等,然后就不需要密码了。再怎么强调也不为过,信任只是开发和测试机器的好选择。永远不应该在生产机器上使用信任。