Skip to content

About

Fairly full featured Ansible role for Postgresql.

Topics

Resources

Stars

936 stars

Watchers

46 watching

Forks

Latest commit

Β 

History

754 Commits

Folders and files

Repository files navigation

ANXS - PostgreSQL Build Status


Help Wanted! If you are able and willing to help maintain this Ansible role then please open a GitHub issue. A lot of people seem to use this role and we (quite obviously) need assistance! πŸ’–

Ansible role which installs and configures PostgreSQL, extensions, databases and users.

Installation

This role requires ansible-core 2.15 or higher.

To install:

ansible-galaxy install anxs.postgresql

Example Playbook

An example how to include this role:

---
- hosts: postgresql-server
  roles:
    - role: ANXS.postgresql
      become: yes

An example how to include this role as a task:

---
- hosts: postgresql-server
  tasks:
    - block: # workaround, see https://stackoverflow.com/a/56558842
        - name: PSQL installation and configuration
          include_role:
            name: ANXS.postgresql
          vars:
            postgresql_users:
              - name: abc
                password: abc
      become: true

Dependencies

  • ANXS.monit (Galaxy/GH) if you want monit protection (in that case, you should set monit_protection: true)

Compatibility matrix

Distribution / PostgreSQL 11 12 13 14 15 16 17 18
Debian 12.x β›” β›” βœ… βœ… βœ… βœ… βœ… βœ…
Debian 13.x β›” β›” βœ… βœ… βœ… βœ… βœ… βœ…
Rockylinux 8.x β›” β›” ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️
Rockylinux 9.x β›” β›” βœ… βœ… βœ… βœ… ⚠️ ⚠️
Ubuntu 22.04.x β›” β›” βœ… βœ… βœ… βœ… βœ… βœ…
Ubuntu 24.04.x β›” β›” βœ… βœ… βœ… βœ… βœ… βœ…
Ubuntu 26.04.x β›” β›” βœ… βœ… βœ… βœ… βœ… βœ…
  • βœ… - works fine
  • ⚠️ - has known issues
  • β›” - is unsupported and/or EOL

Variables

# Basic settings
postgresql_version: 17
postgresql_encoding: "UTF-8"
postgresql_locale: "en_US.UTF-8"
postgresql_ctype: "en_US.UTF-8"
# Data checksums of the clusters the role creates (postgresql_cluster_reset: true).
# null (default): initdb's own default, off up to PostgreSQL 17 and on from 18, like the cluster created by
# the Debian/Ubuntu package. true: --data-checksums. false: --no-data-checksums on 18+, e.g. before a
# pg_upgrade from a cluster without checksums (pg_upgrade needs the same setting on both clusters).
# The cluster created by the package itself follows /etc/postgresql-common/createcluster.conf
# (initdb_options), not this variable.
postgresql_data_checksums: null

postgresql_admin_user: "postgres"
postgresql_default_auth_method: "peer"

postgresql_cluster_name: main
postgresql_cluster_reset: false

# List of databases to be created (optional)
# Note: for more flexibility with extensions use the postgresql_database_extensions setting.
postgresql_databases:
  - name: foobar
    owner: baz          # optional; specify the owner of the database
    hstore: yes         # flag to install the hstore extension on this database (yes/no)
    uuid_ossp: yes      # flag to install the uuid-ossp extension on this database (yes/no)
    citext: yes         # flag to install the citext extension on this database (yes/no)
    encoding: "UTF-8"   # override global {{ postgresql_encoding }} variable per database
    state: "present"    # optional; one of 'present', 'absent', 'dump', 'restore'
    lc_collate: "en_GB.UTF-8"   # override global {{ postgresql_locale }} variable per database
    lc_ctype: "en_GB.UTF-8"     # override global {{ postgresql_ctype }} variable per database

# List of database extensions to be created (optional)
postgresql_database_extensions:
  - db: foobar
    extensions:
      - hstore
      - citext

# List of users to be created (optional)
postgresql_users:
  - name: baz
    pass: pass
    encrypted: yes  # if password should be encrypted, postgresql >= 10 does only accepts encrypted passwords
    state: "present"    # optional; one of 'present', 'absent'

# List of schemas to be created (optional)
postgresql_database_schemas:
  - database: foobar           # database name
    schema: acme               # schema name
    state: present

  - database: foobar           # database name
    schema: acme_baz           # schema name
    owner: baz                 # owner name
    state: present

# List of user privileges to be applied (optional)
postgresql_user_privileges:
  - name: baz                   # user name
    db: foobar                  # database
    priv: "ALL"                 # privilege string format: example: INSERT,UPDATE/table:SELECT/anothertable:ALL
    role_attr_flags: "CREATEDB" # role attribute flags

Extra packages (optional)

The role can add third-party APT/YUM repositories and install packages from them:

postgresql_ext_install_extra_packages: true
postgresql_ext_extra_packages:
  names:
    - pgadmin4-server
  apt_repositories:
    depot-pgadmin:
      uris: "https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/{{ ansible_facts['distribution_release'] }}"
      suites: "pgadmin4"
      components:
        - main
      signed_by: "https://www.pgadmin.org/static/packages_pgadmin_org.pub"
  yum_repositories:
    depot-pgadmin:
      name: pgadmin
      description: pgAdmin 4
      url: "https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/redhat/rhel-9-x86_64"
      gpgkey: "https://www.pgadmin.org/static/packages_pgadmin_org.pub"

APT repositories are deb822-style mappings managed by deb822_repository:

  • uris (required): repository base URL.
  • suites (required): distribution suite.
  • components (optional, default ["main"]).
  • signed_by (recommended): URL or local path to the signing key. ASCII-armored keys are stored as .asc under /etc/apt/keyrings.
  • types (optional, default ["deb"]).

The old one-line format (depot-pgadmin: "deb [...] https://... jammy pgadmin4 main") is no longer accepted; the role stops with a migration message. The old apt_keys map is still used as a fallback source for signed_by, but new configurations should set signed_by directly.

There's a lot more knobs and bolts to set, which you can find in the defaults/main.yml

Testing - Molecule

This project comes with a molecule configuration. Please see ./molecule/README.md

Examples:

molecule test

Testing - Vagrant

This project comes with a Vagrantfile, this is a fast and easy way to test changes to the role, fire it up with vagrant up

See vagrant docs for getting setup with vagrant

Once your VM is up, you can reprovision it using either vagrant provision, or ansible-playbook tests/playbook.yml -i vagrant-inventory

If you want to toy with the test play, see tests/playbook.yml, and change the variables in tests/vars.yml

If you are contributing, please first test your changes within the vagrant environment, (using the targeted distribution), and if possible, ensure your change is covered in the tests found in .travis.yml

License

Licensed under the MIT License. See the LICENSE file for details.

Thanks

Creator:

Maintainers:

Top Contributors:

Feedback, bug-reports, requests, ...

Are welcome!

About

Fairly full featured Ansible role for Postgresql.

Topics

Resources

Stars

936 stars

Watchers

46 watching

Forks

Releases

Packages

Used by

Contributors

Languages