PostgreSQL Block

This block sets up a PostgreSQL database.

Thanks to upstream nixpkgs, SHB supports several versions of PostgreSQL at a time. On top of upstream features, SHB also sets up:

Usage

It is defined in /modules/blocks/postgresql.nix and to use it, add this import:

{ inputs, ... }:
{
  imports = [ inputs.selfhostblocks.nixosModules.default ];
}

Select a PostgreSQL Version

SelfHostBlocks requires an explicit PostgreSQL major version:

shb.postgresql.version = 18;

Note

On first usage of this module, an evaluation error will pop up if this option is not set. Set it to the major version of PostgreSQL you have already deployed. Inspect the running server or its PG_VERSION file.

Setting the option does not upgrade the database. See below for the upgrade procedure. If SHB finds a cluster for a different PostgreSQL major version on activation, it refuses to initialize and reports the required migration steps.

Supported versions are 15, 16, 17, and 18. The server, data directory, backup tools, and restore tools all follow this selection.

Declarative User and Database

Ensure a database and user exists:

shb.postgresql.ensures = [
  {
    username = "firefly-iii";
    database = "firefly-iii";
    passwordFile = "/run/secrets/firefly-iii_db_password";
  }
];

Upgrade PostgreSQL

SHB provides upgrade scripts from the configured version in shb.postgresql.version to every newer supported version.

For example, assuming version 15 is set in the option and SHB supports from versions 15 to 18 included, SHB provides the binaries on the system path upgrade-pg-cluster-15-16, upgrade-pg-cluster-15-17, and upgrade-pg-cluster-15-18. PostgreSQL supports upgrading directly to a newer major version so there is no issue in upgrading straight to latest.

Warning

Please make sure to backup the database before and after each operation. Although a backup can only be taken when the database is running. The safest and quickest is to take instead a filesystem snapshot if available. It has the advantage that it can be done when the database is not running.

  1. If not done yet, set shb.postgresql.version to the major version you have and deploy. This way the upgrade helpers are installed.

  2. Stop the systemd service systemctl stop postgresql.service. Of course, you can stop dependent services but this is technically enough.

  3. Run the upgrade script to the target version: upgrade-pg-cluster-<from>-<to>. I would advise going directly to the latest version, because why not, but maybe some services are not yet compatible so you should check that first. The script will fail if the target directory already exists. You must first remove that directory before retrying but of course make sure nothing valuable is in there first. If unsure, just move it.

  4. If the script failed, nothing needs to be reverted. You can either start the postgresql.service again or try to figure out the issue with the error. In the latter case, when running the script again, it will fail because the target folder folder exists already. Same advice as above: you must remove the target folder first but make sure nothing valuable is in there. If unsure, just move it.

  5. If the script succeeded, do not start the postgresql.service yet as this will start the old database and you might end up with stale data in the new upgraded one.

  6. Set shb.postgresql.version to the targeted version and deploy. This will automatically start the database with the new version.

  7. Inspect dependent services and restart them if needed. Some recover from a database connection loss but some will require a little nudge.

  8. Run the statistics commands emitted by your target version as the postgres operating-system user.

When you assessed everything worked, you can remove the old database directory /var/lib/postgresql/<old>.

Database Backup Requester Contracts

This block can be backed up using the database backup contract.

Contract integration tests are defined in /test/contracts/databasebackup.nix.

Backing up All Databases
{
  my.backup.provider."postgresql" = {
    request = config.shb.postgresql.databasebackup;

    settings = {
      // Specific options for the backup provider.
    };
  };
}

Tests

Specific integration tests are defined in /test/blocks/postgresql.nix.

Options Reference

shb.postgresql.enableTCPIP

Enable TCP/IP connection on given port.

Type: boolean

Default:

false

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup

Backup configuration.

Type: submodule

Default:

{ }

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.request

Request part of the database backup contract.

Options set by the requester module enforcing how to backup files.

Type: submodule

Default:

""

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.request.backupCmd

Command that produces the database dump on stdout.

Type: string

Default:

''
  ''${pkgs.postgresql}/bin/pg_dumpall --clean --if-exists | ''${pkgs.gzip}/bin/gzip --rsyncable
''

Example:

${pkgs.postgresql}/bin/pg_dumpall | ${pkgs.gzip}/bin/gzip --rsyncable

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.request.backupName

Name of the backup in the repository.

Type: string

Default:

"postgres.sql"

Example:

"postgresql.sql"

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.request.restoreCmd

Command that reads the database dump on stdin and restores the database.

Type: string

Default:

''
  ''${pkgs.gzip}/bin/gunzip | sudo -u postgres ''${pkgs.postgresql}/bin/psql
''

Example:

${pkgs.gzip}/bin/gunzip | ${pkgs.postgresql}/bin/psql postgres

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.request.user

Unix user doing the backups.

This should be an admin user having access to all databases.

Type: string

Default:

"postgres"

Example:

"postgres"

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.result

Result part of the database backup contract.

Options set by the provider module that indicates the name of the backup and restore scripts.

Type: submodule

Default:

""

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.result.backupService

Name of service backing up the database.

This script can be ran manually to backup the database:

$ systemctl start backup.service

Type: string

Default:

"backup.service"

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.databasebackup.result.restoreScript

Name of script that can restore the database. One can then list snapshots with:

$ restore snapshots

And restore the database with:

$ restore restore latest

Type: string

Default:

"restore"

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.debug

Enable debugging options.

Currently enables shared_preload_libraries = “auto_explain, pg_stat_statements”

See https://www.postgresql.org/docs/current/pgstatstatements.html

Type: boolean

Default:

false

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.ensures

List of username, database and/or passwords that should be created.

Type: list of (submodule)

Default:

[ ]

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.ensures.*.database

Postgres database.

Type: string

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.ensures.*.passwordFile

Optional password file for the postgres user. If not given, only peer auth is accepted for this user, otherwise password auth is allowed.

Type: null or string

Default:

null

Example:

"/run/secrets/postgresql/password"

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.ensures.*.username

Postgres user name.

Type: string

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>
shb.postgresql.version

PostgreSQL major version managed by SelfHostBlocks. This must be set when PostgreSQL is enabled. The null default produces an evaluation error requiring existing deployments to select their current major and new deployments to choose a supported major.

Type: null or one of 15, 16, 17, 18

Default:

null

Example:

18

Declared by:

<selfhostblocks/modules/blocks/postgresql.nix>