From 1bb1c981b04f8e65f604cfbd43c3fc684b057235 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Tue, 9 Apr 2024 16:27:27 -0500 Subject: [PATCH] Add Postgres upgrade script --- hosts/myself/matrix.nix | 2 ++ modules/nixos/db.nix | 1 + overlays/default.nix | 1 + overlays/upgrade-pg-cluster.nix | 24 ++++++++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 overlays/upgrade-pg-cluster.nix diff --git a/hosts/myself/matrix.nix b/hosts/myself/matrix.nix index bc0b4c5..8dc250d 100644 --- a/hosts/myself/matrix.nix +++ b/hosts/myself/matrix.nix @@ -5,6 +5,8 @@ let conn = "postgresql:///dendrite?sslmode=disable&host=/run/postgresql"; in { + environment.systemPackages = with pkgs; [ upgrade-pg-cluster ]; + greg.containers.matrix = { tailscale = true; subnet = "204"; diff --git a/modules/nixos/db.nix b/modules/nixos/db.nix index 60c4d56..ec551d1 100644 --- a/modules/nixos/db.nix +++ b/modules/nixos/db.nix @@ -18,6 +18,7 @@ in { services = { postgresql = { enable = true; + package = pkgs.postgresql_15; checkConfig = true; ensureDatabases = dbs; ensureUsers = map (db: { name = db; ensureDBOwnership = true; }) dbs; diff --git a/overlays/default.nix b/overlays/default.nix index 0a660f9..648d8cf 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -60,6 +60,7 @@ in rec { setup-ssh = prev.callPackage ./setup-ssh { pkgs = final.pkgs; }; + upgrade-pg-cluster = prev.callPackage ./upgrade-pg-cluster.nix {}; # Overrides of packages brew = prev.callPackage ./homebrew.nix {}; diff --git a/overlays/upgrade-pg-cluster.nix b/overlays/upgrade-pg-cluster.nix new file mode 100644 index 0000000..a22e36e --- /dev/null +++ b/overlays/upgrade-pg-cluster.nix @@ -0,0 +1,24 @@ +{ postgresql_15, postgresql_14, writeScriptBin, ... }: + +let + newPostgres = postgresql_15; + oldPostgres = postgresql_14; +in writeScriptBin "upgrade-pg-cluster" '' + set -eux + systemctl stop postgresql + + export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}" + export NEWBIN="${newPostgres}/bin" + + export OLDDATA="/var/lib/postgresql/${oldPostgres.psqlSchema}" + export OLDBIN="${oldPostgres}/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" \ + "$@" +''