PostgreSQL Block

Table of Contents

Tests
Database Backup Requester Contracts
Options Reference

Defined in /modules/blocks/postgresql.nix.

This block sets up a PostgreSQL database.

Tests

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

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.
    };
  };
}

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. This is an output option.

Use it to initialize a block implementing the “backup” contract. For example, with the restic block:

shb.restic.instances."postgresql" = {
  request = config.shb.postgresl.backup;
  settings = {
    enable = true;
  };
};

Type: submodule

Default:

{
  backupCmd = ''
    ''${pkgs.postgresql}/bin/pg_dumpall | ''${pkgs.gzip}/bin/gzip --rsyncable
  '';
  backupName = "postgres.sql";
  restoreCmd = ''
    ''${pkgs.gzip}/bin/gunzip | ''${pkgs.postgresql}/bin/psql postgres
  '';
  user = "postgres";
}

Declared by:

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

Command that produces the database dump on stdout.

Type: string

Example:

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

Declared by:

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

Name of the backup in the repository.

Type: string

Default: "dump"

Example: "postgresql.sql"

Declared by:

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

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

Type: string

Example:

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

Declared by:

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

Unix user doing the backups.

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

Type: string

Example: "postgres"

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>