From 49f7ed8d60083f53ba4cbdd62c7ecbfb2a300308 Mon Sep 17 00:00:00 2001 From: June Date: Wed, 6 Dec 2023 01:06:43 +0100 Subject: [PATCH] Add and run script for upgrading PostgreSQL of netbox 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/netbox/default.nix | 1 + config/hosts/netbox/postgresql.nix | 33 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 config/hosts/netbox/postgresql.nix diff --git a/config/hosts/netbox/default.nix b/config/hosts/netbox/default.nix index 5fdb479..d4a02cf 100644 --- a/config/hosts/netbox/default.nix +++ b/config/hosts/netbox/default.nix @@ -6,5 +6,6 @@ ./netbox.nix ./networking.nix ./nginx.nix + ./postgresql.nix ]; } diff --git a/config/hosts/netbox/postgresql.nix b/config/hosts/netbox/postgresql.nix new file mode 100644 index 0000000..9c13e59 --- /dev/null +++ b/config/hosts/netbox/postgresql.nix @@ -0,0 +1,33 @@ +{ pkgs, config, ... }: + +{ + # 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 netbox.service netbox-rq.service + systemctl stop redis-netbox.service + systemctl stop postgresql.service + + 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 \ + "$@" + '') + ]; +}