From 0944a1749c63d88c091bd0ad2c4abe593834d684 Mon Sep 17 00:00:00 2001 From: June Date: Wed, 6 Dec 2023 00:46:47 +0100 Subject: [PATCH] Add and run script for upgrading PostgreSQL of matrix host The script is a modified version of the script shown in the PostgreSQL Upgrading section of the NixOS manual. Our version is for upgrading PostgreSQL 14 to 15. Basically do steps 1-3 of the section. Link to the section: https://nixos.org/manual/nixos/stable/#module-services-postgres-upgrading --- config/hosts/matrix/postgresql.nix | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/config/hosts/matrix/postgresql.nix b/config/hosts/matrix/postgresql.nix index 20bcdd5..9cfdb46 100644 --- a/config/hosts/matrix/postgresql.nix +++ b/config/hosts/matrix/postgresql.nix @@ -14,4 +14,33 @@ dataDir = "/mnt/data/postgresql/${config.services.postgresql.package.psqlSchema}"; }; + + # Modified version of the script for upgrading PostgreSQL from here: + # https://nixos.org/manual/nixos/stable/#module-services-postgres-upgrading + environment.systemPackages = [ + (let + newPostgres = pkgs.postgresql_15; + in pkgs.writeScriptBin "upgrade-pg-cluster" '' + set -eux + + systemctl stop matrix-synapse.service + systemctl stop postgresql.service + + export NEWDATA="/mnt/data/postgresql/${newPostgres.psqlSchema}" + + export NEWBIN="${newPostgres}/bin" + + export OLDDATA="${config.services.postgresql.dataDir}" + export OLDBIN="${config.services.postgresql.package}/bin" + + install -d -m 0700 -o postgres -g postgres "$NEWDATA" + cd "$NEWDATA" + sudo -u postgres $NEWBIN/initdb -D "$NEWDATA" + + sudo -u postgres $NEWBIN/pg_upgrade \ + --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \ + --old-bindir $OLDBIN --new-bindir $NEWBIN \ + "$@" + '') + ]; }