From 8671964043ad06b214015764cb553a826e79fc98 Mon Sep 17 00:00:00 2001 From: Lillian-Violet Date: Thu, 21 Dec 2023 20:38:49 +0100 Subject: [PATCH] Add upgrade path for postgresql --- nixos/queen/configuration.nix | 1 + nixos/upgrade/postgresql.nix | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 nixos/upgrade/postgresql.nix diff --git a/nixos/queen/configuration.nix b/nixos/queen/configuration.nix index 4444710..c2a81bf 100644 --- a/nixos/queen/configuration.nix +++ b/nixos/queen/configuration.nix @@ -21,6 +21,7 @@ ./mail-server.nix ./webmail.nix ./gotosocial.nix + ../upgrade/postgresql.nix #./akkoma.nix ]; diff --git a/nixos/upgrade/postgresql.nix b/nixos/upgrade/postgresql.nix new file mode 100644 index 0000000..d683f3f --- /dev/null +++ b/nixos/upgrade/postgresql.nix @@ -0,0 +1,36 @@ +{ + config, + pkgs, + ... +}: { + environment.systemPackages = [ + (let + # XXX specify the postgresql package you'd like to upgrade to. + # Do not forget to list the extensions you need. + newPostgres = pkgs.postgresql_15.withPackages (pp: [ + # pp.plv8 + ]); + in + pkgs.writeScriptBin "upgrade-pg-cluster" '' + set -eux + # XXX it's perhaps advisable to stop all services that depend on postgresql + systemctl stop postgresql + + export NEWDATA="/var/lib/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 \ + "$@" + '') + ]; +}