使用 brew 安装的 PostgreSQL 服务,不知道什么时候升级了版本,在启动时失败了,第一次也没有报错。
使用数据库连接工具进行连接时报错无法连接,使用 services info
查看服务信息时,发现也是没有 Running
$ brew services info postgresqlpostgresql (homebrew.mxcl.postgresql)Running: ✘Loaded: ✔Schedulable: ✘
在尝试执行多次启动命令后,出现了报错信息:
$ brew services start postgresqlBootstrap failed: 5: Input/output errorError: 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.log2022-03-28 08:44:12.638 CST [66363] FATAL: database files are incompatible with server2022-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), HEADObject-relational database systemhttps://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:12From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/postgresql.rbLicense: PostgreSQL==> DependenciesBuild: pkg-config ✔Required: icu4c ✘, krb5 ✘, openssl@1.1 ✘, readline ✘==> Options--HEADInstall HEAD version==> CaveatsTo migrate existing data from a previous major version of PostgreSQL run:brew postgresql-upgrade-databaseThis formula has created a default database cluster with:initdb --locale=C -E UTF-8 /usr/local/var/postgresFor more details, read:https://www.postgresql.org/docs/14/app-initdb.htmlTo restart postgresql after an upgrade:brew services restart postgresqlOr, if you don't want/need a background service you can just run:/usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres==> Analyticsinstall: 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 54322022-03-28 09:09:25.578 CST [78258] LOG: listening on IPv4 address "127.0.0.1", port 54322022-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 CST2022-03-28 09:09:26.551 CST [78259] LOG: database system was not properly shut down; automatic recovery in progress2022-03-28 09:09:26.556 CST [78259] LOG: redo starts at 0/16FDD182022-03-28 09:09:26.556 CST [78259] LOG: invalid record length at 0/16FDDF8: wanted 24, got 02022-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 doneserver startedwaiting for server to shut down....2022-03-28 09:09:27.094 CST [78258] LOG: received fast shutdown request2022-03-28 09:09:27.094 CST [78258] LOG: aborting any active transactions2022-03-28 09:09:27.095 CST [78258] LOG: background worker "logical replication launcher" (PID 78265) exited with exit code 12022-03-28 09:09:27.095 CST [78260] LOG: shutting down2022-03-28 09:09:27.100 CST [78258] LOG: database system is shut down doneserver 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 ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default time zone ... Asia/Shanghaicreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connectionsYou 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 okChecking database user is the install user okChecking database connection settings okChecking for prepared transactions okChecking for system-defined composite types in user tables okChecking for reg* data types in user tables okChecking for contrib/isn with bigint-passing mismatch okChecking for user-defined encoding conversions okChecking for user-defined postfix operators okChecking for tables WITH OIDS okChecking for invalid "sql_identifier" user columns okCreating dump of global objects okCreating dump of database schemas okChecking for presence of required libraries okChecking database user is the install user okChecking for prepared transactions okChecking for new cluster tablespace directories okIf pg_upgrade fails after this point, you must re-initdb thenew cluster before continuing.Performing Upgrade------------------Analyzing all rows in the new cluster okFreezing all rows in the new cluster okDeleting files from new pg_xact okCopying old pg_xact to new server okSetting oldest XID for new cluster okSetting next transaction ID and epoch for new cluster okDeleting files from new pg_multixact/offsets okCopying old pg_multixact/offsets to new server okDeleting files from new pg_multixact/members okCopying old pg_multixact/members to new server okSetting next multixact ID and offset for new cluster okResetting WAL archives okSetting frozenxid and minmxid counters in new cluster okRestoring global objects in the new cluster okRestoring database schemas in the new cluster okCopying user relation files okSetting next OID for new cluster okSync data directory to disk okCreating script to delete old cluster okChecking for extension updates okUpgrade 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-stagesRunning 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
,说明迁移完成并已经启动,再次连接就可以了。