PostgreSQL

The PostgreSQL service installed with brew, which was upgraded at some point, failed at startup, and no error was reported the first time.

When I use the database connection tool to connect, I get an error that I cannot connect. When I use services info to check the service information, I also find that there is no Running.

1
2
3
4
5
$ brew services info postgresql
postgresql (homebrew.mxcl.postgresql)
Running: ✘
Loaded: ✔
Schedulable: ✘

After trying to execute the start command several times, an error message appears.

1
2
3
$ 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.

Go to the postgresql error log and look for useful information.

1
2
3
4
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.

The error message is probably that the data directory was initialized by PostgreSQL version 11 and is not compatible with the current version 13.2.

I remember that I installed PostgreSQL 11 before, how come it is now 13.2? I ran brew info postgresql to check it out.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)

It did get upgraded, so what should I do now? Look closely at the output in info for one paragraph.

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

Meaning to migrate existing data from a previous major version of PostgreSQL, run.

1
brew postgresql-upgrade-database

Then execute it, execute it and start the migration, wait for the execution to finish…

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
==> 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)

When you see Successfully started postgresql at the end, it means that the migration is complete and has been started, just connect again.