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:
Enabling TCP/IP login and also accepting password authentication from localhost with
shb.postgresql.enableTCPIP.Enhance the
ensure*upstream option by setting up a database’s password from a password file withshb.postgresql.ensures.Debug logging with
auto_explainandpg_stat_statementswithshb.postgresql.debug.Automatic backups using the backup contracts with
shb.postgresql.databasebackup.Explicit PostgreSQL major-version selection with
shb.postgresql.version.
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.
If not done yet, set
shb.postgresql.versionto the major version you have and deploy. This way the upgrade helpers are installed.Stop the systemd service
systemctl stop postgresql.service. Of course, you can stop dependent services but this is technically enough.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.If the script failed, nothing needs to be reverted. You can either start the
postgresql.serviceagain 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.If the script succeeded, do not start the
postgresql.serviceyet as this will start the old database and you might end up with stale data in the new upgraded one.Set
shb.postgresql.versionto the targeted version and deploy. This will automatically start the database with the new version.Inspect dependent services and restart them if needed. Some recover from a database connection loss but some will require a little nudge.
Run the statistics commands emitted by your target version as the
postgresoperating-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:
falseDeclared 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 --rsyncableDeclared 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 postgresDeclared 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.serviceType: 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 snapshotsAnd restore the database with:
$ restore restore latestType: 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:
falseDeclared 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:
nullExample:
"/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:
nullExample:
18Declared by:
<selfhostblocks/modules/blocks/postgresql.nix>