Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Upgrading PostgreSQL to a major version in Archlinux

Upgrading PostgreSQL to a major version in Archlinux

# pacman -S postgresql-old-upgrade

# systemctl stop postgresql.service
# mv /var/lib/postgres/data /var/lib/postgres/olddata

# mkdir /var/lib/postgres/data /var/lib/postgres/tmp

# chown postgres:postgres /var/lib/postgres/data /var/lib/postgres/tmp
# su - postgres

[postgres]$ initdb -D '/var/lib/postgres/data'

[postgres]$ cd /var/lib/postgres/tmp

[postgres]$ pg_upgrade -b /opt/pgsql-`OLD_PG_VERSION`/bin -B /usr/bin -d /var/lib/postgres/olddata -D /var/lib/postgres/data

# systemctl start postgresql

[postgres]$ ./

Logout from postgres user and execute

# /var/lib/postgres/tmp/

or manually do

# rm -rf /var/lib/postgres/olddata

Once the upgrade is complete you may delete the `/var/lib/postgres/tmp` directory

# rm -rf /var/lib/postgres/tmp

Errors when upgrading to a major version

* template0 must not allow connections, i.e. its pg_database.datallowconn must
be false

* connection to database failed: FATAL:  database "template1" is not currently
accepting connections

This happens when the column datallowconn from pg_database is set to true in template0 and in template1 is set to false

[postgres] > SELECT datname, datallowconn FROM pg_database;

            datname             | datallowconn
 template0                      | t
 template1                      | f


# su - postgres

$ /opt/pgsql-`PG_OLD_VERSION`/bin/pg_ctl -D /var/lib/postgres/olddata/ start

$ /opt/pgsql-`PG_OLD_VERSION`/bin/psql -U pg-user -d postgres

[postgres] > UPDATE pg_database SET datallowconn='false' WHERE datname='template0';

[postgres] > UPDATE pg_database SET datallowconn='true' WHERE datname='template1';

$ /opt/pgsql-`PG_OLD_VERSION`/bin/pg_ctl -D /var/lib/postgres/olddata/ stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment