重新启动MacBook Pro后,我无法启动数据库服务器:

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

我检查了日志,下面的行出现了一遍又一遍:

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

9.0.4是mac上预装的版本。4]是我通过Homebrew安装的版本。

如前所述,这在重新启动之前是可以工作的,因此它实际上不可能是编译问题。我还重新运行了initdb /usr/local/var/postgres -E utf8,文件仍然存在。


当前回答

如果你正在寻找核心选项(删除所有数据并获得一个新的数据库),你可以这样做:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

然后你需要rake db:setup和rake db:migrate from你的Rails app来重新安装。

其他回答

postgres会给你提示,比如从以前的主版本的PostgreSQL中迁移现有数据:

在我的例子中,删除旧的rm -rf /usr/local/var/ postgresql。DB brew postgresql-upgrade-database

试试这个: https://gist.github.com/joho/3735740

这对我来说非常有效。 最后,它还生成2个bash脚本来检查数据库并删除旧的集群。 真的太棒了。

参见:http://www.postgresql.org/docs/9.2/static/pgupgrade.html了解更多。

类似于这些答案(1,2),我的Postgres数据库文件在升级到postgresql 13.3后与我的Postgres版本不兼容。

不幸的是,升级Postgres数据目录失败了。

$ brew postgresql-upgrade-database
...
Setting next OID for new cluster
*failure*

Consult the last few lines of "pg_upgrade_utility.log" for
the probable cause of the failure.
Failure, exiting
Error: Upgrading postgresql data from 12 to 13 failed!
==> Removing empty postgresql initdb database...
==> Moving postgresql data back from /usr/local/var/postgres.old to /usr/local/var/postgres...
Error: Failure while executing; `/usr/local/opt/postgresql/bin/pg_upgrade -r -b /usr/local/Cellar/postgresql@12/12.7/bin -B /usr/local/opt/postgresql/bin -d /usr/local/var/postgres.old -D /usr/local/var/postgres -j 8` exited with 1.

我的解决办法是重新安装postgresql 12.7。

$ brew reinstall postgresql@12
$ brew services start postgresql@12

如果你最近将postgres升级到最新版本,你可以运行下面的命令来升级你的postgres数据目录,保留所有数据:

brew postgresql-upgrade-database

上面的命令取自brew info postgres的输出

注意:根据最近的测试,这将不适用于从14升级到15。

当在Docker中运行新的Postgres时,旧的卷没有更新,也会发生这种情况。

如果你不需要保存你的数据,最简单的方法是清除docker文件夹中的旧卷,在Linux中:

/var/lib/docker/volumes