PostgreSQL on OpenBSD: Upgrade 12 to 13 with pg_dumpall

nabbisen - May 8 '21 - - Dev Community

* The cover image is originally by Andy_Bay and edited with great appreciation.


Summary

In upgrading OpenBSD from 6.8 to 6.9, I had to upgrade PostgreSQL server (12.6 to 13.2), as written in OpenBSD's Upgrade Guide.
Here is how I carried it out due to the official readme of postgresql-server package:

$ nvim -R /usr/local/share/doc/pkg-readmes/postgresql-server
Enter fullscreen mode Exit fullscreen mode

Alternatively, using pg_upgrade might make the migration process faster as it is necessary to install the package in addition.

Tutorial

First, check the server is running.

$ doas rcctl check postgresql
postgresql(ok)
Enter fullscreen mode Exit fullscreen mode

Run pg_dumpall.

$ doas su _postgresql -c "cd /var/postgresql \
    && pg_dumpall -U postgres > /var/postgresql/full.sqldump.bkYYMMDD"
$ # pg superuser's password is required times of the number of tables
Enter fullscreen mode Exit fullscreen mode

Besides, "YYMMDD" here may be replaced with the actual date which is got, for example, with date +"%y%m%d" in Fish shell.

Stop the server.

$ doas rcctl stop postgresql
postgresql(ok)
$ # check the server is stopped
$ doas rcctl check postgresql
postgresql(failed)
Enter fullscreen mode Exit fullscreen mode

Upgrade the server, the package.

$ doas pkg_add -ui postgresql-server
$ # updated
Enter fullscreen mode Exit fullscreen mode

Renew /var/postgresql/data.

$ doas mv /var/postgresql/data /var/postgresql/data-12
$ doas su _postgresql -c "mkdir /var/postgresql/data"
Enter fullscreen mode Exit fullscreen mode

Run initdb.

$ # `--locale` option here can be omitted
$ doas su _postgresql -c "cd /var/postgresql && \
    initdb -D /var/postgresql/data -U postgres -A scram-sha-256 -E UTF8 -W \
        --locale=xx_XX.UTF-8"
$ # pg superuser's password is required
Enter fullscreen mode Exit fullscreen mode

Optionally, configure the server again as needed.

$ # in case of using tls/ssl connection
$ # - set `listen_addresses` and `ssl` option
$ doas nvim "/var/postgresql/data/postgresql.conf"
$ # - add `hostssl` authentication
$ doas nvim "/var/postgresql/data/pg_hba.conf"
$ # - create certificate if not exists
$ doas su _postgresql -c \
    "cp /var/postgresql/data-12/server.{crt,key} /var/postgresql/data/"
Enter fullscreen mode Exit fullscreen mode

It is based on:


Start the new server.

$ doas rcctl start postgresql
postgresql(ok)
$ # check the server is started and running
$ doas rcctl check postgresql
postgresql(ok)
Enter fullscreen mode Exit fullscreen mode

Restore databases, objects such as tables and records.

$ doas su _postgresql -c "cd /var/postgresql && \
    psql -U postgres < /var/postgresql/full.sqldump.bkYYMMDD"
$ # restored
Enter fullscreen mode Exit fullscreen mode

Done 🙂

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player