{"id":14973979,"url":"https://github.com/fidanf/ansible-role-postgresql-ha","last_synced_at":"2025-10-02T01:31:33.955Z","repository":{"id":44660777,"uuid":"222653040","full_name":"fidanf/ansible-role-postgresql-ha","owner":"fidanf","description":"Install and configure PostgreSQL cluster managed with repmgr. Add dependencies, extensions, databases and users.","archived":false,"fork":true,"pushed_at":"2024-01-31T11:17:27.000Z","size":641,"stargazers_count":21,"open_issues_count":1,"forks_count":24,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-28T18:22:08.817Z","etag":null,"topics":["high-availability","pgsql","postgresql","replication","repmgr"],"latest_commit_sha":null,"homepage":"","language":"Jinja","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"Demonware/postgresql","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fidanf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-19T09:03:15.000Z","updated_at":"2024-08-14T13:06:26.000Z","dependencies_parsed_at":"2023-01-23T10:47:24.132Z","dependency_job_id":null,"html_url":"https://github.com/fidanf/ansible-role-postgresql-ha","commit_stats":null,"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fidanf%2Fansible-role-postgresql-ha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fidanf%2Fansible-role-postgresql-ha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fidanf%2Fansible-role-postgresql-ha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fidanf%2Fansible-role-postgresql-ha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fidanf","download_url":"https://codeload.github.com/fidanf/ansible-role-postgresql-ha/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234922825,"owners_count":18907828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["high-availability","pgsql","postgresql","replication","repmgr"],"created_at":"2024-09-24T13:49:46.442Z","updated_at":"2025-10-02T01:31:28.667Z","avatar_url":"https://github.com/fidanf.png","language":"Jinja","funding_links":[],"categories":[],"sub_categories":[],"readme":"## PostgreSQL HA\n\n[![Build Status](https://travis-ci.com/fidanf/ansible-role-postgresql-ha.svg?branch=master)](https://travis-ci.com/fidanf/ansible-role-postgresql-ha)\n\nInstall and configure a PostgreSQL high-availability cluster managed with repmgr. Add dependencies, extensions, databases and users. Works for standalone installations as well.\n\nTested with :\n  - Debian 10.x :heavy_check_mark:\n  - Debian 11.x :heavy_check_mark:\n  - Ubuntu 18.04.x :heavy_check_mark:\n  - Ubuntu 20.04.x :heavy_check_mark:\n\n---\n\n- [PostgreSQL HA](#postgresql-ha)\n  - [Requirements](#requirements)\n  - [Installation](#installation)\n  - [Dependencies](#dependencies)\n  - [Example Inventory](#example-inventory)\n  - [Role variables](#role-variables)\n  - [Example Playbook](#example-playbook)\n- [Usefull commands to run after your first installation](#usefull-commands-to-run-after-your-first-installation)\n  - [Verifying cluster functionality](#verifying-cluster-functionality)\n  - [Show cluster status](#show-cluster-status)\n  - [List nodes and their attributes](#list-nodes-and-their-attributes)\n  - [Register (clone) an additionnal standby node](#register-clone-an-additionnal-standby-node)\n- [Register former primary as a standby node after automatic failover](#register-former-primary-as-a-standby-node-after-automatic-failover)\n- [License](#license)\n\n### Requirements\n\n- Python \u003e=3.8\n- Ansible-core \u003e=2.12\n\nSee [./requirements.txt](./requirements.txt) for detailled dependencies used to develop the role.\n\n**Recommended, for each postgresql host**\n- Python3 in PATH\n- Pip3 in PATH\n- [psycopg2-binary](https://pypi.org/project/psycopg2-binary/) (itself requires libpq-dev apt-package)\n\nYou can take a look at [prepare.yml](molecule/default/prepare.yml) to check out an example setup for Python 3.\n\n### Installation\n\nShell\n```bash\nansible-galaxy collection install community.crypto\nansible-galaxy install fidanf.postgresql_ha\n```\n\nRequirements file\n```yaml\n---\nroles:\n- src: https://github.com/fidanf/ansible-role-postgresql-ha\n  name: fidanf.postgresql_ha\n  version: master\n\ncollections:\n  - community.crypto\n\n```\n\n### Dependencies \n\n- [cryptography](https://pypi.org/project/cryptography/) (ansible host)\n\n### Example Inventory\n\nSee [inventory.yml](./inventory.example.yml) for detailed group and host vars.\n\n### Role variables\n\nRole default variables are split amongst two files :\n  - [001-postgresql.yml](./defaults/main/001-postgresql.yml)\n  - [002-repmgr.yml](./defaults/main/002-repmgr.yml)\n\nIn order to exactly figure out the purpose and valid values for each of these variables, do not hesitate to inspect all the Jinja templates in the [templates](./templates) directory. Original default configuration files are also included (`.orig`).\n\n### Example Playbook\n\n```yaml\n---\n- hosts: pgcluster\n  gather_facts: yes\n  become: yes\n  roles:\n    - name: fidanf.postgresql_ha\n      vars:\n        # Required configuration items\n        repmgr_target_group: pgcluster\n        repmgr_master: pgsql01\n        repmgr_failover: automatic\n        repmgr_promote_command: /usr/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file\n        repmgr_follow_command: /usr/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n\n        repmgr_monitoring_history: \"yes\"\n        repmgr_connection_check_type: query\n        repmgr_log_level: DEBUG\n        repmgr_reconnect_attempts: 2\n        repmgr_reconnect_interval: 10\n        # Basic settings\n        postgresql_version: 13\n        postgresql_cluster_name: main\n        postgresql_cluster_reset: false # TODO: Needs to be tested for repmgr\n        postgresql_listen_addresses: \"*\"\n        postgresql_port: 5432\n        postgresql_wal_level: \"replica\"\n        postgresql_max_wal_senders: 10\n        postgresql_max_replication_slots: 10\n        postgresql_wal_keep_segments: 100\n        postgresql_hot_standby: on\n        postgresql_archive_mode: on\n        postgresql_archive_command: \"test ! -f /tmp/%f \u0026\u0026 cp %p /tmp/%f\"\n        postgresql_ext_install_repmgr: yes\n        postgresql_shared_preload_libraries:\n          - repmgr\n        # postgresql logging \n        postgresql_log_checkpoints: on\n        postgresql_log_connections: on\n        postgresql_log_disconnections: on\n        postgresql_log_temp_files: 0\n        # pg_hba.conf\n        postgresql_pg_hba_custom:\n          - { type: \"host\", database: \"all\", user: \"all\", address: \"192.168.56.0/24\", method: \"md5\" }\n          - { type: \"host\", database: \"replication\", user: \"{{ repmgr_user }}\", address: \"192.168.56.0/24\", method: \"trust\" }  \n          - { type: \"host\", database: \"replication\", user: \"{{ repmgr_user }}\", address: \"127.0.0.1/32\", method: \"trust\" }  \n          - { type: \"host\", database: \"{{ repmgr_database }}\", user: \"{{ repmgr_user }}\", address: \"127.0.0.1/32\", method: \"trust\" }  \n          - { type: \"host\", database: \"{{ repmgr_database }}\", user: \"{{ repmgr_user }}\", address: \"192.168.56.0/32\", method: \"trust\" }  \n        # Databases\n        postgresql_databases:\n          - name: \"{{ repmgr_database }}\"\n            owner: \"{{ repmgr_user }}\"\n            encoding: \"UTF-8\"\n          - name: testdb\n            owner: admin\n            encoding: \"UTF-8\"\n        # Users\n        postgresql_users:\n          - name: \"{{ repmgr_user }}\"\n            pass: \"{{ repmgr_password }}\"\n          - name: admin\n            pass: secret # postgresql \u003e=10 does not accept unencrypted passwords\n            encrypted: yes\n        # Roles\n        postgresql_user_privileges:\n          - name: \"{{ repmgr_user }}\"\n            db: \"{{ repmgr_database }}\"\n            priv: \"ALL\"\n            role_attr_flags: \"SUPERUSER,REPLICATION\"\n          - name: admin\n            db: testdb\n            role_attr_flags: \"SUPERUSER\"\n\n```\n\n## Usefull commands to run after your first installation\n\n### Verifying cluster functionality\n\n```bash\nansible pgcluster -b --become-user postgres -m shell -a \"repmgr cluster crosscheck\"\n```\n\n### Show cluster status\n\n```bash\nansible pgcluster -b --become-user postgres -m shell -a \"repmgr cluster show\"\n```\n\n### List nodes and their attributes\n\n```bash\nansible pgcluster -b --become-user postgres -m shell -a \"repmgr node status\"\n```\n\n### Register (clone) an additionnal standby node \n\n```bash\n# Assuming the current primary hostname is pgsql01\nansible-playbook myplaybook.yml -l 'pgsql04' -e 'repmgr_primary_hostname=pgsql01' -vv \n```\n\n## Register former primary as a standby node after automatic failover\n\n```\npostgres@pgsql01:~$ pg_ctlcluster 13 main stop\npostgres@pgsql01:~$ repmgr standby clone --force -h pgsql02 -U repmgr -d repmgr\npostgres@pgsql01:~$ pg_ctlcluster 13 main start\npostgres@pgsql01:~$ repmgr standby register --force\n```\n\nOr you may use the repmgr node rejoin with [pg_rewind](https://repmgr.org/docs/current/repmgr-node-rejoin.html#REPMGR-NODE-REJOIN-PG-REWIND) \n\n```bash\nrepmgr node rejoin -d repmgr -U repmgr -h pgsql02 --verbose --force-rewind=/usr/lib/postgresql/13/bin/pg_rewind\n```\n\n## License\n\nMIT / BSD\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffidanf%2Fansible-role-postgresql-ha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffidanf%2Fansible-role-postgresql-ha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffidanf%2Fansible-role-postgresql-ha/lists"}