为什么 psql 无法连接到服务器?

Why psql could not connect to server?

我之前使用 Homebrew 在 Mac 上安装了 Postgresql。

当我尝试访问数据库时,不断出现以下错误:

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

与我从 psql 收到的错误消息相比,有没有办法获得更好的错误消息?

是的。

不幸的是,当 Homebrew 服务启动时,它可能会在您没有意识到的情况下悄无声息地失败。

为了确认这也是你的情况,运行:

brew services list

您应该看到 PostgreSQL 的状态为 started。但是,颜色是 黄色,而不是 绿色(根据您的配色方案,可能有点难看)。

黄色表示;实际状态是 unknown 而不是 started!

要使 unknown 状态有意义,请使用 pg_ctl 启动 PostgreSQL 服务器:

# For Intel
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

# For M1:
pg_ctl -D /opt/homebrew/var/postgresql@11 start

这个命令应该输出真正的问题:

dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.63.dylib
  Referenced from: /usr/local/Cellar/postgresql/10.6_1/bin/postgres
  Reason: image not found
no data was returned by command ""/usr/local/Cellar/postgresql/10.6_1/bin/postgres" -V"
The program "postgres" is needed by pg_ctl but was not found in the
same directory as "/usr/local/Cellar/postgresql/10.6_1/bin/pg_ctl".
Check your installation.

对于上面的示例,icu4c 库的版本。