Playbook
Pigsty has a series of playbooks for PostgreSQL:
pgsql.yml: Init HA PostgreSQL clusters or add new replicas.pgsql-rm.yml: Remove PostgreSQL cluster, or remove replicaspgsql-user.yml: Add new business user to existing PostgreSQL clusterpgsql-db.yml: Add new business database to existing PostgreSQL clusterpgsql-monitor.yml: Monitor remote PostgreSQL instance with local exporterspgsql-migration.yml: Generate Migration manual & scripts for existing PostgreSQL
Safeguard
Beware, when using the pgsql.yml and pgsql-rm.yml playbooks, it can pose a risk of accidentally deleting databases if misused!
- When using
pgsql.yml, please check the--tags|-tand--limit|-lparameters. - Adding the -l parameter when executing playbooks is strongly recommended to limit execution hosts.
- Think thrice before proceeding.
To prevent accidental deletions, the PGSQL module offers a safeguard option controlled by the following two parameters:
pg_safeguardis set tofalseby default: do not prevent purging by default.pg_cleanis set totrueby default, meaning it will clean existing instances.
Effects on the init playbook
When meeting a running instance with the same config during the execution of the pgsql.yml playbook:
pg_safeguard / pg_clean |
pg_clean=true |
pg_clean=false |
|---|---|---|
pg_safeguard=false |
Purge | Abort |
pg_safeguard=true |
Abort | Abort |
- If
pg_safeguardis enabled, the playbook will abort to avoid purging the running instance. - If the safeguard is disabled, it will further decide whether to remove the existing instance according to the value of
pg_clean.- If
pg_cleanistrue, the playbook will directly clean up the existing instance to make room for the new instance. This is the default behavior. - If
pg_cleanisfalse, the playbook will abort, which requires explicit configuration.
- If
Effects on the remove playbook
When meeting a running instance with the same config during the execution of the pgsql-rm.yml playbook:
pg_safeguard / pg_clean |
pg_clean=true |
pg_clean=false |
|---|---|---|
pg_safeguard=false |
Purge & rm data | Purge |
pg_safeguard=true |
Abort | Abort |
- If
pg_safeguardis enabled, the playbook will abort to avoid purging the running instance. - If the safeguard is disabled, it purges the running instance and will further decide whether to remove the existing data along with the instance according to the value of
pg_clean.- If
pg_cleanistrue, the playbook will directly clean up the PostgreSQL data cluster. - If
pg_cleanisfalse, the playbook will skip data purging, which requires explicit configuration.
- If
pgsql.yml
The pgsql.yml is used for init HA PostgreSQL clusters or adding new replicas.
This playbook contains following subtasks:
Administration Tasks that use this playbook
Some notes about this playbook
When running this playbook on a single replica, You should make sure the cluster primary is already initialized.
- you may have to run
Reload HBARuleandAppend Replicaafter replica init. - The wrap script
pgsql-addwill do this, check SOP: Add Instance for details.
pgsql-rm.yml
The playbook pgsql-rm.yml can remove PostgreSQL cluster, or specific replicas from cluster.
This playbook contains following subtasks:
Some arguments can affect the behavior of this playbook:
Administration Tasks that use this playbook
Some notes about this playbook
Do not run this playbook on single cluster primary directly when there are still replicas.
- otherwise the rest replicas will trigger automatic failover.
- It won’t be a problem if you remove all replicas before removing primary.
- If you run this on the entire cluster, you don’t have to worry about this.
Reload service after removing replicas from cluster
- When a replica is removed, it is still in the configuration file of the haproxy load balancer.
- It is a dead server, so it won’t affect the cluster service.
- But you should reload service in time to ensure the consistency between the environment and the config inventory.
pgsql-user.yml
The playbook pgsql-user.yml can add new business user to existing PostgreSQL cluster.
Check admin SOP: Create User
pgsql-db.yml
The playbook pgsql-db.yml can add new business database to existing PostgreSQL cluster.
Check admin SOP: Create Database
pgsql-monitor.yml
The playbook pgsql-monitor.yml can monitor remote postgres instance with local exporters.
Check admin SOP: Monitor Postgres
pgsql-migration.yml
The playbook pgsql-migration.yml can generate migration manual & scripts for existing PostgreSQL cluster.
Check admin SOP: Migration