Skip to content

Commit

Permalink
services: postgres: add upgrade script
Browse files Browse the repository at this point in the history
  • Loading branch information
alarsyo committed Dec 2, 2024
1 parent ddc4893 commit 3f6b882
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions services/postgresql.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
{
config,
pkgs,
lib,
...
}: {
# set postgresql version so we don't get any bad surprise
config.services.postgresql = {
package = pkgs.postgresql_15;
};

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_16;
cfg = config.services.postgresql;
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="${cfg.dataDir}"
export OLDBIN="${cfg.package}/bin"
install -d -m 0700 -o postgres -g postgres "$NEWDATA"
cd "$NEWDATA"
sudo -u postgres $NEWBIN/initdb -D "$NEWDATA" ${lib.escapeShellArgs cfg.initdbArgs}
sudo -u postgres $NEWBIN/pg_upgrade \
--old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
--old-bindir $OLDBIN --new-bindir $NEWBIN \
"$@"
'')
];
}

0 comments on commit 3f6b882

Please sign in to comment.