Created
May 26, 2021 14:46
-
-
Save thefloweringash/dac83887172b5c15b76eeebd3e47ee00 to your computer and use it in GitHub Desktop.
upgrade-postgres.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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 \ | |
"$@" | |
'' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like the manual, but adjusted to be a simple shell script.