Code前端首页关于Code前端联系我们

有关如何修复在 macOS 上启动 PostgreSQL 服务失败的说明

terry 2年前 (2023-09-26) 阅读数 44 #数据库
macOS 下 PostgreSQL 服务启动失败解决手记

PostgreSQL 服务是使用 beer 安装的。不知道什么时候更新版本,启动失败。最初没有报告错误。

使用数据连接工具连接时,出现错误,连接失败。使用service 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 ✘, openssl@1.1 ✘, 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)

检查结束Postgresql启动成功,说明迁移完成并开始,重新连接即可。

作者:沈献之

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门