galaxyproject / ansible-postgresql

An Ansible role for managing a PostgreSQL server
https://galaxy.ansible.com/galaxyproject/postgresql
122 stars 58 forks source link

PostgreSQL

An Ansible role for installing and managing PostgreSQL servers. This role works with both Debian and RedHat based systems, and provides backup scripts for PostgreSQL Continuous Archiving and Point-in-Time Recovery. It does not create or manage PostgreSQL users, roles, groups, databases, and so forth. For that, see galaxyproject.postgresql_objects.

On RedHat-based platforms, the PostgreSQL Global Development Group (PGDG) packages packages will be installed. On Debian-based platforms, you can choose from the distribution's packages (from APT) or the PGDG packages.

Changes that require a restart will not be applied unless you manually restart PostgreSQL. This role will reload the server for those configuration changes that can be updated with only a reload because reloading is a non-intrusive operation, but options that require a full restart will not cause the server to restart.

Requirements

This role requires Ansible 2.4+

Role Variables

All variables are optional

Backups

This role can deploy and schedule the configuration and scripts to maintain Postgresql PITR backups.

Full backups will be made on the configured interval, whereas write-ahead-log (WAL) segments between full backups will be archived to {{ postgresql_backup_dir }}/wal_archive/ when instructed by the PostgreSQL server. WAL segments can be removed from this directory once the oldest backup referencing them has been removed. This is done automatically for you by the backup script if postgresql_backup_dir is mounted locally.

When postgresql_backup_dir is a remote rsync path (containing a ":"), the backup script will still maintain backups (including deleting older full backups) but cannot prune the wal_archive/ directory automatically. If you are able to install the standard pg_archivecleanup utility from the PostgreSQL client package on your backup server, you can run this role's backup script with the --clean-archive option directly on the backup server instead.

Additional options pertaining to backups can be found in the defaults file.

Dependencies

Backup functionality requires Python 2.7 or 3.5+, psycopg2, and rsync. Note that if installing PGDG versions of PostgreSQL on Enterprise Linux, corresponding psycopg2 packages are available from the PGDG yum repositories.

Example Playbook

Standard install: Default postgresql.conf, pg_hba.conf and default version for the OS:

---

- hosts: dbservers
  roles:
    - galaxyproject.postgresql

Use the pgdg packages on a Debian-based host:

---

- hosts: dbservers
  vars:
    postgresql_flavor: pgdg
  roles:
    - galaxyproject.postgresql

Use the PostgreSQL 9.5 packages and set some postgresql.conf options and pg_hba.conf entries:

---

- hosts: dbservers
  vars:
    postgresql_version: 9.5
    postgresql_conf:
      - listen_addresses: "''"    # disable network listening (listen on unix socket only)
      - max_connections: 50       # decrease connection limit
    postgresql_pg_hba_conf:
      - host all all 10.0.0.0/8 md5
  roles:
    - galaxyproject.postgresql

Enable backups to /archive:

- hosts: all
  vars:
    postgresql_backup_dir: /archive
  roles:
    - galaxyproject.postgresql

Enable backups to /archive on a remote server:

- hosts: dbservers
  vars:
    postgresql_backup_dir: backup.example.org:/archive
  roles:
    - galaxyproject.postgresql

- hosts: backupservers
  tasks:
    - name: Install PostgreSQL scripts
      ansible.builtin.apt:
        name: postgresql-common
    - name: Copy backup script
      ansible.builtin.copy:
        src: roles/galaxyproject.postgresql/files/backup.py
        dest: /usr/local/bin/pgbackup.py
        mode: "0755"
    - name: Schedule WAL pruning
      ansible.builtin.cron:
        name: Prune PostgreSQL Archived WALs
        hour: 22
        minute: 0
        job: /usr/local/bin/pgbackup.py --clean-archive /archive

License

Academic Free License ("AFL") v. 3.0

Author Information

The Galaxy Community and contributors