diff --git a/nixos/modules/services/databases/postgresql.xml b/nixos/modules/services/databases/postgresql.xml index 07af4c937f03..0ca9f3faed21 100644 --- a/nixos/modules/services/databases/postgresql.xml +++ b/nixos/modules/services/databases/postgresql.xml @@ -52,37 +52,51 @@ Type "help" for help.
Upgrading + + + The steps below demonstrate how to upgrade from an older version to pkgs.postgresql_13. + These instructions are also applicable to other versions. + + - Major PostgreSQL upgrade requires PostgreSQL downtime and a few imperative steps to be called. To simplify this process, use the following NixOS module: + Major PostgreSQL upgrades require a downtime and a few imperative steps to be called. This is the case because + each major version has some internal changes in the databases' state during major releases. Because of that, + NixOS places the state into /var/lib/postgresql/<version> where each version + can be obtained like this: - containers.temp-pg.config.services.postgresql = { - enable = true; - package = pkgs.postgresql_12; - ## set a custom new dataDir - # dataDir = "/some/data/dir"; - }; - environment.systemPackages = - let newpg = config.containers.temp-pg.config.services.postgresql; - in [ - (pkgs.writeScriptBin "upgrade-pg-cluster" '' - set -x - export OLDDATA="${config.services.postgresql.dataDir}" - export NEWDATA="${newpg.dataDir}" - export OLDBIN="${config.services.postgresql.package}/bin" - export NEWBIN="${newpg.package}/bin" +$ nix-instantiate --eval -A postgresql_13.psqlSchema +"13" + + For an upgrade, a script like this can be used to simplify the process: + +{ config, pkgs, ... }: +{ + = [ + (pkgs.writeScriptBin "upgrade-pg-cluster" '' + set -eux + # XXX it's perhaps advisable to stop all services that depend on postgresql + systemctl stop postgresql - install -d -m 0700 -o postgres -g postgres "$NEWDATA" - cd "$NEWDATA" - sudo -u postgres $NEWBIN/initdb -D "$NEWDATA" + # XXX replace `<new version>` with the psqlSchema here + export NEWDATA="/var/lib/postgresql/<new version>" - systemctl stop postgresql # old one + # XXX specify the postgresql package you'd like to upgrade to + export NEWBIN="${pkgs.postgresql_13}/bin" - sudo -u postgres $NEWBIN/pg_upgrade \ - --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \ - --old-bindir $OLDBIN --new-bindir $NEWBIN \ - "$@" - '') - ]; + export OLDDATA="${config.}" + export OLDBIN="${config.}/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 \ + "$@" + '') + ]; +} @@ -103,17 +117,25 @@ Type "help" for help. - Run upgrade-pg-cluster. It will stop old postgresql, initialize new one and migrate old one to new one. You may supply arguments like --jobs 4 and --link to speedup migration process. See for details. + Run upgrade-pg-cluster. It will stop old postgresql, initialize a new one and migrate the old one to the new one. You may supply arguments like --jobs 4 and --link to speedup migration process. See for details. - Change postgresql package in NixOS configuration to the one you were upgrading to, and change dataDir to the one you have migrated to. Rebuild NixOS. This should start new postgres using upgraded data directory. + Change postgresql package in NixOS configuration to the one you were upgrading to via . Rebuild NixOS. This should start new postgres using upgraded data directory and all services you stopped during the upgrade. - After upgrade you may want to ANALYZE new db. + After the upgrade it's advisable to analyze the new cluster (as su -l postgres in the + , in this example /var/lib/postgresql/13): + +$ ./analyze_new_cluster.sh + + The next step removes the old state-directory! + +$ ./delete_old_cluster.sh +