PostgreSQL在Mac上无法启动的问题解决

今天启动我一个Rails App的时候遇到错误:

connection to server at "::1", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

看来是psql没有运行起来,或者是被谁shutdown了,执行 postgres -D /usr/local/var/postgres 出现以下提示

2022-02-11 20:29:45.999 CST [25264] FATAL:  database files are incompatible with server
2022-02-11 20:29:45.999 CST [25264] DETAIL:  The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.1.

看来又是哪个地方不小心把本地的postgresql升级了,导致数据不兼容:数据库里面的数据是13版本创建的,现在psql软件版本是14,两者之间不兼容。解决办法是升级数据库中的数据格式为14版本的格式。
执行 brew postgresql-upgrade-database 对数据库中的数据进行升级。途中遇到一个问题:

There seems to be a postmaster servicing the new cluster.
Please shutdown that postmaster and try again.
Failure, exiting
Error: Upgrading postgresql data from 13 to 14 failed!
==> Removing empty postgresql initdb database...
==> Moving postgresql data back from /opt/homebrew/var/postgres.old to /opt/homebrew/var/postgres...
Error: Failure while executing; `/opt/homebrew/opt/postgresql/bin/pg_upgrade -r -b /opt/homebrew/Cellar/postgresql@13/13.5_1/bin -B /opt/homebrew/opt/postgresql/bin -d /opt/homebrew/var/postgres.old -D /opt/homebrew/var/postgres -j 8` exited with 1.

执行 brew services stop postgresql,把正在运行的实例关掉,在执行 brew postgresql-upgrade-database 问题解决了。

Leave a Reply