Skip to content

Instantly share code, notes, and snippets.

@thefloweringash
Created May 26, 2021 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefloweringash/dac83887172b5c15b76eeebd3e47ee00 to your computer and use it in GitHub Desktop.
Save thefloweringash/dac83887172b5c15b76eeebd3e47ee00 to your computer and use it in GitHub Desktop.
upgrade-postgres.nix
{ pkgs ? import <nixpkgs> {}
, old ? "postgresql_9_6"
, new ? "postgresql_12"
}:
let
oldPkg = pkgs."${old}";
newPkg = pkgs."${new}";
in
pkgs.writeShellScript "upgrade-pg-cluster" ''
set -euo pipefail
set -x
export OLDDATA="/var/lib/postgresql/${oldPkg.psqlSchema}"
export NEWDATA="/var/lib/postgresql/${newPkg.psqlSchema}"
export OLDBIN="${oldPkg}/bin"
export NEWBIN="${newPkg}/bin"
if [ ! -e "$OLDDATA/PG_VERSION" ]; then
echo "Old data dir invalid"
exit 1
fi
install -d -m 0700 -o postgres -g postgres "$NEWDATA"
cd "$NEWDATA"
sudo -u postgres $NEWBIN/initdb -D "$NEWDATA"
systemctl stop postgresql # old one
sudo -u postgres $NEWBIN/pg_upgrade \
--old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
--old-bindir $OLDBIN --new-bindir $NEWBIN \
"$@"
''
@thefloweringash
Copy link
Author

Like the manual, but adjusted to be a simple shell script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment