helm/hosts/profiles/postgres_upgrade_script/default.nix

31 lines
947 B
Nix
Raw Permalink Normal View History

2022-10-03 06:59:21 -07:00
{ config, pkgs, ... }:
{
environment.systemPackages = [
(pkgs.writeScriptBin "upgrade-pg-cluster" ''
set -eux
# TODO it's perhaps advisable to stop all services that depend on postgresql
systemctl stop postgresql
# The schema can be found by running:
2024-03-15 23:25:49 -07:00
# nix eval --raw nixpkgs#postgresql_15.psqlSchema
export NEWDATA="/var/lib/postgresql/15"
2022-10-03 06:59:21 -07:00
# TODO specify the postgresql package you'd like to upgrade to
2024-03-15 23:25:49 -07:00
export NEWBIN="${pkgs.postgresql_15}/bin"
2022-10-03 06:59:21 -07:00
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 \
"$@"
'')
];
}