解决 macOS 下 PostgreSQL 服务启动失败

使用 brew 安装的 PostgreSQL 服务,不知道什么时候升级了版本,在启动时失败了,第一次也没有报错。

使用数据库连接工具进行连接时报错无法连接,使用services info查看服务信息时,发现也是没有Running

$ brew services info postgresql
postgresql (homebrew.mxcl.postgresql)
Running: ✘
Loaded: ✔
Schedulable: ✘

在尝试执行多次启动命令后,出现了报错信息:

$ brew services start postgresql
Bootstrap failed: 5: Input/output error
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/lufei/Library/LaunchAgents/homebrew.mxcl.postgresql.plist` exited with 5.

去查看 postgresql 的错误日志,查找有用的信息:

tail -n 100 /usr/local/var/log/postgres.log

2022-03-28 08:44:12.638 CST [66363] FATAL:  database files are incompatible with server
2022-03-28 08:44:12.638 CST [66363] DETAIL:  The data directory was initialized by PostgreSQL version 11, which is not compatible with this version 13.2.

错误信息大概是数据目录是由 PostgreSQL 11 版本初始化的,与当前版本 13.2 版本不兼容。

我记得之前安装的是 11 ,怎么变成 13.2 了?执行brew info postgresql查看了一下

postgresql: stable 14.2 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
/usr/local/Cellar/postgresql/13.2_2 (3,218 files, 42.6MB) *
Poured from bottle on 2021-05-07 at 14:38:12
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/postgresql.rb
License: PostgreSQL
==> Dependencies
Build: pkg-config ✔
Required: icu4c ✘, krb5 ✘, [email protected] ✘, readline ✘
==> Options
--HEAD
Install HEAD version
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database

This formula has created a default database cluster with:
initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql after an upgrade:
brew services restart postgresql
Or, if you don't want/need a background service you can just run:
/usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
==> Analytics
install: 154,721 (30 days), 391,708 (90 days), 1,475,468 (365 days)
install-on-request: 150,064 (30 days), 378,837 (90 days), 1,413,606 (365 days)
build-error: 30 (30 days)

确实是被升级了,那现在应该怎么办呢?仔细看info中输出的内容有一段:

To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database

意思是要从以前的 PostgreSQL 主要版本中迁移现有数据,请运行:

brew postgresql-upgrade-database

那就执行呗,执行后就开始迁移了,等待执行完成...

==> Upgrading postgresql data from 11 to 14...
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
waiting for server to start....2022-03-28 09:09:25.578 CST [78258] LOG:  listening on IPv6 address "::1", port 5432
2022-03-28 09:09:25.578 CST [78258] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-03-28 09:09:25.579 CST [78258] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-03-28 09:09:25.607 CST [78259] LOG:  database system was interrupted; last known up at 2020-06-10 19:50:23 CST
2022-03-28 09:09:26.551 CST [78259] LOG:  database system was not properly shut down; automatic recovery in progress
2022-03-28 09:09:26.556 CST [78259] LOG:  redo starts at 0/16FDD18
2022-03-28 09:09:26.556 CST [78259] LOG:  invalid record length at 0/16FDDF8: wanted 24, got 0
2022-03-28 09:09:26.556 CST [78259] LOG:  redo done at 0/16FDDC0
.2022-03-28 09:09:26.564 CST [78258] LOG:  database system is ready to accept connections
 done
server started
waiting for server to shut down....2022-03-28 09:09:27.094 CST [78258] LOG:  received fast shutdown request
2022-03-28 09:09:27.094 CST [78258] LOG:  aborting any active transactions
2022-03-28 09:09:27.095 CST [78258] LOG:  background worker "logical replication launcher" (PID 78265) exited with exit code 1
2022-03-28 09:09:27.095 CST [78260] LOG:  shutting down
2022-03-28 09:09:27.100 CST [78258] LOG:  database system is shut down
 done
server stopped
==> Moving postgresql data from /usr/local/var/postgres to /usr/local/var/postgres.old...
==> Creating database...
The files belonging to this database system will be owned by user "lufei".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/Cellar/postgresql/14.2_1/bin/pg_ctl -D /usr/local/var/postgres -l logfile start

==> Migrating and upgrading data...
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting oldest XID for new cluster                          ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to delete old cluster                       ok
Checking for extension updates                              ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/local/Cellar/postgresql/14.2_1/bin/vacuumdb --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
==> Upgraded postgresql data from 11 to 14!
==> Your postgresql 11 data remains at /usr/local/var/postgres.old
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

看到最后的Successfully started postgresql,说明迁移完成并已经启动,再次连接就可以了。

10 条评论

发表评论

*