为什么在将其作为服务运行时无法访问postgres?

tim*_*xyz 5 postgresql macos psql

当我尝试通过webapp访问postgres,或打开postgres shell时psql -d template1,我收到错误.

psql:无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受Unix域套接字"/tmp/.s.PGSQL.5432"上的连接?

(*我已经阅读了很多有关此错误的SO主题,但推荐的解决方案并未解决此问题)

将postgres作为服务启动:

brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)

停止postgresql...(可能需要一段时间)==>已成功停止postgresql(标签:homebrew.mxcl.postgresql)==>已成功启动postgresql(标签:homebrew.mxcl.postgresql)

在终端中手动运行postgres:

我可以通过在终端窗口中运行来完成所有工作,但我更喜欢在后台运行它.

postgres -D /usr/local/var/postgres9.6.3
Run Code Online (Sandbox Code Playgroud)

集群:

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

postgres
postgres9.5/
postgres9.6.3/
Run Code Online (Sandbox Code Playgroud)

哪一个:

which psql 回报 /usr/local/bin/psql

路径:

echo $PATH返回/usr/local/bin/usr/local/var(添加)

其他推荐解决方案

很多人能够通过删除a来修复此问题/postgres/postmaster.pid,但此文件对我来说并不存在.

UPDATE

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

501  2135  1530   0 12:08pm ttys002    0:00.00 grep postgres
Run Code Online (Sandbox Code Playgroud)

重启postgres后brew servies,postgres.log/usr/local/var/logcontains中,

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.
Run Code Online (Sandbox Code Playgroud)

Vao*_*sun 3

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

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

如果您选择第一个选项,则无需执行进一步的步骤 - 您的默认版本是 9.6,它将适用于现有数据目录。

在运行之前pg_upgrade,制作 data_directory 的冷副本