infra/README.postgres.md

1.4 KiB

Postgres container

Update Postgres to new major version (UNTESTED!)

here 16 => 17

  1. Stop services that use postgres or cut connection by change postgres port
  2. Login as postgres user
sudo -su postgres
old=16
cd /var/lib/postgresql/
pg_old=$(nix-build --no-out-link -A postgresql_${old:?} '<nixpkgs>')
pg_new=$(nix-build --no-out-link -A postgresql_$((old+1)) '<nixpkgs>')
  1. Initialize new data directory
$pg_new/bin/initdb --encoding=UTF8 --locale=C $((old+1))
  1. Run check
$pg_new/bin/pg_upgrade \
    --old-bindir=$pg_old/bin \
    --new-bindir=$pg_new/bin \
    --old-datadir=/var/lib/postgresql/${old:?} \
    --new-datadir=/var/lib/postgresql/$((old+1)) \
    --clone \
    --check
  1. Stop the old Postgres
systemctl stop postgresql
  1. Run the migration
$pg_new/bin/pg_upgrade \
    --old-bindir=$pg_old/bin \
    --new-bindir=$pg_new/bin \
    --old-datadir=/var/lib/postgresql/${old:?} \
    --new-datadir=/var/lib/postgresql/$((old+1)) \
    --clone
  1. Start the new Postgres
# Change 'services.postgres.packages = pkgs.postgresql_17;' in services/postgres.nix
nixos-rebuild switch

Cleanup (after a few days):

sudo -su postgres
vacuumdb --all --analyze-in-stages
cd /var/lib/postgresql/
./delete_old_cluster.sh
rm delete_old_cluster.sh

Build with ❤️ and ❄️.