1.4 KiB
1.4 KiB
Postgres container
Update Postgres to new major version (UNTESTED!)
here 16 => 17
- Stop services that use postgres or cut connection by change postgres port
- 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>')
- Initialize new data directory
$pg_new/bin/initdb --encoding=UTF8 --locale=C $((old+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
- Stop the old Postgres
systemctl stop postgresql
- 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
- 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 ❄️.