nix-infra/config/hosts/netbox/postgresql.nix

34 lines
1,016 B
Nix
Raw Normal View History

{ 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 \
"$@"
'')
];
}