当 运行 将 postgres 作为一项服务时,为什么我无法访问它?

Why can I not access postgres when running it as a service?

当我尝试通过网络应用程序访问 postgres,或使用 psql -d template1 打开 postgres shell 时,出现错误。

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"?

(*我已经阅读了许多关于此错误的 SO 线程,但推荐的解决方案并没有为我解决此问题)

将 postgres 作为服务启动:

brew services restart postgresql

Stopping postgresql... (might take a while) ==> Successfully stopped postgresql (label: homebrew.mxcl.postgresql) ==> Successfully started postgresql (label: homebrew.mxcl.postgresql)

在终端中手动 运行 postgres:

我可以通过下面的 运行 在终端 window 中使一切正常工作,但我更愿意 运行 它在后台运行。

postgres -D /usr/local/var/postgres9.6.3

集群:

我在 /usr/local/var 中有 3 个数据库集群,但我想使用 postgres9.6.3/

postgres
postgres9.5/
postgres9.6.3/

其中:

which psql returns /usr/local/bin/psql

路径:

echo $PATH returns /usr/local/bin/usr/local/var(已添加)

其他推荐方案:

很多人可以通过删除 /postgres/postmaster.pid 来解决这个问题,但我不存在这个文件。

更新

运行 ps -ef | grep postgres,仅输出,

501  2135  1530   0 12:08pm ttys002    0:00.00 grep postgres

使用 brew servies 重新启动 postgres 后,/usr/local/var/log 中的 postgres.log 包含,

FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.5, which is not compatible with this version 9.6.3.

服务定义仍在指定您的旧 9.5 数据目录。如果你打开 ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist 你应该看到一个看起来像这样的部分:

<key>ProgramArguments</key>
<array>
  <string>/usr/local/opt/postgresql/bin/postgres</string>
  <string>-D</string>
  <string>/usr/local/var/postgres9.5</string>
</array>

更改最后一个参数以指向您的 9.6.3 数据目录并重新启动服务。如果不需要,您可能需要注销并重新登录,以便重新读取 plist。

如果你想在 9.6 服务器上使用你的 9.5 数据。你必须 运行:

  • pg_upgrade(here) 或
  • pg_dump 从 9.5 到文件和 pg_restore | psql 取决于 9.6 上的备份类型... (here)

如果您选择第一个选项,则无需其他步骤 - 您的默认版本为 9.6,它将与现有数据目录一起使用。

在运行宁pg_upgrade之前,冷拷贝data_directory