Ansible role to setup PostgreSQL
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Simon Cornet 013c9dcacb
All checks were successful
ci/woodpecker/push/linting Pipeline was successful
docs: improve readme
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-05 17:28:26 +02:00
.woodpecker chore(package): update cr.simoncor.net/dockerhub/davidanson/markdownlint-cli2 docker tag to v0.23.3 2026-09-30 12:33:06 +00:00
defaults feat(postgresql): add postgresql_timescaledb_hold option 2026-10-01 12:57:17 +02:00
handlers feat: add postgresql role 2026-09-30 13:13:04 +02:00
meta feat: add postgresql role 2026-09-30 13:13:04 +02:00
roles feat: add postgresql role 2026-09-30 13:13:04 +02:00
tasks feat(postgresql): add postgresql_timescaledb_hold option 2026-10-01 12:57:17 +02:00
templates/postgresql feat: add postgresql role 2026-09-30 13:13:04 +02:00
vars feat: add postgresql role 2026-09-30 13:13:04 +02:00
.ansible-lint feat: add postgresql role 2026-09-30 13:13:04 +02:00
.editorconfig feat: add postgresql role 2026-09-30 13:13:04 +02:00
.gitattributes feat: add postgresql role 2026-09-30 13:13:04 +02:00
.gitignore feat: add postgresql role 2026-09-30 13:13:04 +02:00
.markdownlint-cli2.jsonc feat: add postgresql role 2026-09-30 13:13:04 +02:00
.sops.yaml feat: add postgresql role 2026-09-30 13:13:04 +02:00
.yamllint feat: add postgresql role 2026-09-30 13:13:04 +02:00
AGENTS.md feat(postgresql): add postgresql_timescaledb_hold option 2026-10-01 12:57:17 +02:00
ansible.cfg feat: add postgresql role 2026-09-30 13:13:04 +02:00
playbook.yaml feat: add postgresql role 2026-09-30 13:13:04 +02:00
readme.md docs: improve readme 2026-10-05 17:28:26 +02:00
renovate.json feat: add postgresql role 2026-09-30 13:13:04 +02:00

Ansible Role: PostgreSQL

Opinionated installation and configuration of PostgreSQL, including databases, users, pg_hba.conf rules and optionally TimescaleDB.

Requirements

Operating System Version
Debian 13
Ubuntu 24.04 LTS

The PostgreSQL version is the one shipped by the distribution (Debian 13: 17, Ubuntu 24.04: 16). Set postgresql_version together with postgresql_pgdg_enabled to use another major version from postgresql.org.

The role never upgrades a cluster between major versions. That is a manual step (pg_upgradecluster).

Dependencies

None.

Variables

Variable Required Default Description
postgresql_version No "" Major version to install; empty uses the distribution version
postgresql_pgdg_enabled No false Add the postgresql.org (pgdg) apt repository
postgresql_listen_addresses No "localhost" Value of listen_addresses
postgresql_port No "5432" Value of port
postgresql_config No { max_connections: 100 } Extra postgresql.conf settings, see below
postgresql_hba_rules No [] Extra pg_hba.conf rules, see HBA rules
postgresql_timescaledb_enabled No false Add the Timescale repository, install TimescaleDB and preload it
postgresql_timescaledb_hold No false Hold the TimescaleDB packages, see TimescaleDB hold
postgresql_users No [] Database users to create, see Users
postgresql_databases No [] Databases (and extensions) to create, see Databases

postgresql_config is rendered to /etc/postgresql/<major>/main/conf.d/90-ansible.conf. Booleans and numbers are written as is, other values are quoted. With postgresql_timescaledb_enabled, shared_preload_libraries is set to timescaledb unless you set it yourself in postgresql_config.

Users

Key Required Default Description
name Yes Role name
password No not set Password (sops-encrypted var)
role_attr_flags No NOSUPERUSER,NOCREATEDB,NOCREATEROLE Role attribute flags

Databases

Key Required Default Description
name Yes Database name
owner No postgres Database owner
encoding No cluster default Encoding
lc_collate No cluster default Collation
lc_ctype No cluster default Character classification
template No cluster default Template database
extensions No [] Extensions to create in the DB

HBA rules

Key Required Default Description
source Yes Address or CIDR
contype No host Connection type
users No all User(s) the rule applies to
databases No all Database(s) the rule applies
method No scram-sha-256 Authentication method

Example

Secrets such as database passwords belong in sops-encrypted inventory vars.

postgresql_timescaledb_enabled: true

postgresql_config:
  max_connections: 100
  shared_buffers: "256MB"

postgresql_users:
  - name: "zabbix"
    password: "CHANGE-ME"

postgresql_databases:
  - name: "zabbix"
    owner: "zabbix"
    extensions:
      - "timescaledb"

TimescaleDB hold

The TimescaleDB packages bundle the libraries of several recent extension versions, and older ones get dropped over time. If your database extension is behind the package (for example because Zabbix does not support the newest version yet), set postgresql_timescaledb_hold: true so an apt upgrade cannot remove the library the database still needs. When it is false the role releases the hold. Update the extension first (ALTER EXTENSION timescaledb UPDATE), then release the hold.

Remote access

By default PostgreSQL only listens on localhost. To allow other hosts:

postgresql_listen_addresses: "*"
postgresql_hba_rules:
  - source: "192.168.10.0/24"
    users: "zabbix"
    databases: "zabbix"

Tags

If you call the role without tags, it will execute all of the stages below.

Tags Purpose
postgresql_install Only manage PostgreSQL (and TimescaleDB)
postgresql_config Only manage configuration and HBA rules
postgresql_users Only manage database users
postgresql_databases Only manage databases and their extensions

Usage

Run the role through Semaphore using playbook.yaml. The playbook first runs ansible-galaxy install -f -r roles/requirements.yml on localhost, then executes the postgresql role.