Summary
OpenBSD gives us good documentation as a series of the project. (For example, the man pages are cared and kept maintained.) It's applied to principal service packages as well as the OS. So is as to PostgreSQL.
Well, according to their pkg-readme on PostgreSQL (/usr/local/share/doc/pkg-readmes/postgresql-server
), they suggest we have two ways to upgrade the database:
The former is slower so is not suitable for big databases, but doesn't require the additional package. The latter is vice versa.
I wrote about the former, in which I upgraded PostgreSQL 12 to 13. It is the turn of the latter.
Environment
Tutorial
Here is a step-by-step guide. Let's begin.
Backup (Optional)
When you worry about backup, pg_dumpall
is available:
$ pg_dumpall > pg.sqldump
Here you will be asked by Password:
at the number of databases.
Install pg_upgrade
Get it via the ports package system:
$ doas pkg_add postgresql-pg_upgrade
The result was:
quirks-6.42 signed on 2023-04-06T19:16:59Z
postgresql-pg_upgrade-14.5:postgresql-previous-13.5p0: ok
postgresql-pg_upgrade-14.5: ok
Upgrade OBSD 7.2 to 7.3 and the PG package
It is about upgrading OpenBSD instead of PostgreSQL.
Run sysupgrade
, sysmerge
and pkg_add -u
in this order.
The detail is in this post.
Well, within the process above, you will be asked about PostgreSQL like below:
postgresql-server-15.2p0: Updating across a major version - data migration needed, see the pkg-readme.
Do you want to update now ? [y/N/a] y
Enter "y", and the package (not the database itself) of PostgreSQL will be upgraded. It will be printed out like below:
postgresql-client+postgresql-contrib+postgresql-pg_upgrade+postgresql-server-14.5->postgresql-client-15.2+postgresql-contrib-15.2+postgresql-pg_upgrade-15.2+postgresql-server-15.2p0: ok
(...)
New and changed readme(s):
/usr/local/share/doc/pkg-readmes/postgresql-server
Stop the daemon
Get ready by stopping the server:
$ doas rcctl stop postgresql
The result was:
postgresql(ok)
Now we are ready. Let's upgrade the database !!
Create PostgreSQL 15 cluster
Prepare for creating the data
directory.
Move the current:
$ doas mv /var/postgresql/data /var/postgresql/data-14
Then mkdir
it and initdb
for PostgreSQL:
$ # doas su _postgresql -c "mkdir /var/postgresql/data && cd /var/postgresql && \
initdb -D /var/postgresql/data -U postgres -A scram-sha-256 -E UTF8 -W"
$ doas su _postgresql -c "mkdir /var/postgresql/data && cd /var/postgresql && \
initdb -D /var/postgresql/data -U postgres -A scram-sha-256 -E UTF8 --locale=ja_JP.UTF-8 -W"
The former above is due to the pkg-readme, and the latter is specific for my case, in which --locale=(...)
option is added.
The result was:
The files belonging to this database system will be owned by user "_postgresql".
This user must also own the server process.
The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
Enter new superuser password:
Enter it again:
Enter the password of your PostgreSQL superuser twice. Then it will be followed by the below:
fixing permissions on existing directory /var/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 20
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
pg_ctl -D /var/postgresql/data -l logfile start
Yay. The database is initiated with them generated.
Configure for migration
The steps here are temporary for the data migration.
Edit pg_hba.conf
of the current (and also the next):
$ doas nvim /var/postgresql/data-14/pg_hba.conf
in order to allow local connection by the superuser:
# TYPE DATABASE USER ADDRESS METHOD
+ local all postgres trust
Note that you should put the line at the top in order to let it be the most highly prioritized.
Then copy the files below including pg_hba.conf
to the data
directory of 15, the latest:
$ doas cp -p /var/postgresql/data-14/pg_hba.conf /var/postgresql/data/
$ # in addition, in case that tls is used:
$ doas cp -p /var/postgresql/data-14/server.{crt,key} /var/postgresql/data/
Run pg_upgrade
OK. Your database is now waiting for the upgrade !!! Run the command lines (thanks to the software and the pkg-readme):
$ doas su _postgresql -c "cd /var/postgresql && \
pg_upgrade -b /usr/local/bin/postgresql-14/ -B /usr/local/bin \
-U postgres -d /var/postgresql/data-14/ -D /var/postgresql/data"
The result was below in my case:
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
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/bin/vacuumdb -U postgres --all --analyze-in-stages
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
Successful :)
Restore configuration
Restore the configuration by removing the temporary line:
$ doas nvim /var/postgresql/data/pg_hba.conf
$ # in addition, if you care about restoring the previous one:
$ doas nvim /var/postgresql/data-14/pg_hba.conf
like below:
# TYPE DATABASE USER ADDRESS METHOD
- local all postgres trust
Configure for PostgreSQL 15 (Optional)
When you have specific configuration with postgresql.conf
etc., apply them:
$ # create the backup (optional):
$ doas cp -p /var/postgresql/data/postgresql.conf /var/postgresql/data/postgresql.conf.org
$ doas nvim /var/postgresql/data/postgresql.conf
like below (for example):
(...)
+ listen_addresses = '*'
(...)
+ ssl = on
(...)
Besides, as to postgresql.conf
, the original one is in /usr/local/share/postgresql/postgresql.conf.sample
.
All on the upgrade have been done.
Start the daemon again
Let's start the database server:
$ doas rcctl start postgresql
I hope the result is successful :)
Remove the package (Optional)
You are now free to farewell with appreciation to the package which worked fine:
$ doas pkg_delete postgresql-pg_upgrade
The result was:
postgresql-pg_upgrade-15.2: ok
Conclusion
Through the steps above, we can upgrade PostgreSQL 14 to 15 with upgrading OpenBSD 7.2 to 7.3.
Besides, the data-14
directory may become "old" one in some future.
Wish you pretty happy time with the latest PostgreSQL on the latest OpenBSD 🌟