psql:致命:角色"postgres"不存在(使用-h localhost选项)

Hen*_*hiu 5 postgresql database-connection

我在本地机器上安装了Postgres.

当我执行

psql -U postgres -d buzzsumo
Run Code Online (Sandbox Code Playgroud)

它正确地向用户postgres询问我的密码.

但是,当我跑

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

它给了我这个错误信息:

psql:致命:角色"postgres"不存在

为什么会出现这种情况,在第一个示例中,我连接到本地服务器,这相当于传入localhost?

bor*_*ris 3

你没有说,但我猜你正在 MacOS 或 Linux 上运行 postgres。

第一种形式(不带 -h)通过“unix 套接字”连接。第二种形式,使用“-h localhost”通过 TCP/IP 连接到 localhost(IP 地址 127.0.0.1)。

Postgres 以不同的方式对待这两种类型的连接 - 或者至少它可以这样做。如果您检查“pgsql/data/pg_hba.conf”文件以查看为两种类型的连接配置了哪些身份验证机制。