Be extra cautious when using PGSQL playbooks. Misuse of pgsql.yml and pgsql-rm.yml can lead to accidental database deletion!
Always add the -l parameter to limit the execution scope, and ensure you’re executing the right tasks on the right targets.
Limiting scope to a single cluster is recommended. Running pgsql.yml without parameters in production is a high-risk operation—think twice before proceeding.
Before removal, inspect pig pg list <cluster> and pig pb info, verify a recent backup, and have the operator enter the exact target.
To prevent accidental deletion, Pigsty’s PGSQL module provides a safeguard mechanism controlled by the pg_safeguard parameter.
When pg_safeguard is set to true, the pgsql-rm.yml playbook will abort immediately, protecting your database cluster.
# Will abort execution, protecting data./pgsql-rm.yml -l pg-test -e pg_safeguard=true# Override the safeguard switch from the command line./pgsql-rm.yml -l pg-test -e pg_safeguard=false
In addition to pg_safeguard, pgsql-rm.yml provides finer-grained control parameters:
Do not run this playbook on a primary that still has replicas—otherwise, remaining replicas will trigger automatic failover. Always remove all replicas first, then remove the primary. This is not a concern when removing the entire cluster at once.
Refresh cluster services after removing instances. When you remove a replica from a cluster, it remains in the load balancer configuration file. Since health checks will fail, the removed instance won’t affect cluster services. However, you should Reload Service at an appropriate time to ensure consistency between the production environment and configuration inventory.
pgsql-user.yml
The pgsql-user.yml playbook is used to add new business users to existing PostgreSQL clusters.
Generate user creation SQL at /pg/tmp/pg-user-{{ user.name }}.sql
Execute user creation/update SQL on the cluster primary
If pgbouncer_enabled: true, update /etc/pgbouncer/userlist.txt and useropts.txt
Reload pgbouncer to apply configuration
User Definition Example
pg_users:- name:dbuser_meta # Required, username is the only mandatory fieldpassword:DBUser.Meta # Optional, can be scram-sha-256 hash or plaintextlogin:true# Optional, can login, default: truesuperuser:false# Optional, is superuser, default: falsecreatedb:false# Optional, can create database, default: falsecreaterole:false# Optional, can create role, default: falseinherit:true# Optional, inherit privileges, default: truereplication:false# Optional, can replicate, default: falsebypassrls:false# Optional, bypass RLS, default: falsepgbouncer:true# Optional, add to pgbouncer userlist, default: falseconnlimit:-1# Optional, connection limit, -1 means unlimitedexpire_in:3650# Optional, expire in N days (overrides expire_at)expire_at:'2030-12-31'# Optional, specify expiration datecomment:pigsty admin user # Optional, user commentroles:[dbrole_admin] # Optional, roles to grantparameters:{}# Optional, role-level parameterspool_mode:transaction # Optional, pgbouncer user-level pool modepool_connlimit:100# Optional, user-level max connections; omitted values inherit global default 100
First, define pg_exporters in the infra group variables:
infra:hosts:10.10.10.10:pg_exporters:# List all remote instances, assign unique unused local ports20001:{pg_cluster:pg-foo, pg_seq:1, pg_host:10.10.10.10}20002:{pg_cluster:pg-foo, pg_seq:2, pg_host:10.10.10.11}
The pgsql-migration.yml playbook generates migration manuals and scripts for zero-downtime logical replication-based migration of existing PostgreSQL clusters.
The pgsql-pitr.yml playbook performs PostgreSQL Point-In-Time Recovery (PITR).
Basic Usage
# Recover to latest state (end of WAL archive stream)./pgsql-pitr.yml -l pg-meta -e '{"pg_pitr": {}}'# Recover to specific point in time./pgsql-pitr.yml -l pg-meta -e '{"pg_pitr": {"time": "2025-07-13 10:00:00+00"}}'# Recover to specific LSN./pgsql-pitr.yml -l pg-meta -e '{"pg_pitr": {"lsn": "0/4001C80"}}'# Recover to specific transaction ID./pgsql-pitr.yml -l pg-meta -e '{"pg_pitr": {"xid": "250000"}}'# Recover to named restore point./pgsql-pitr.yml -l pg-meta -e '{"pg_pitr": {"name": "some_restore_point"}}'# Recover from another cluster's backup./pgsql-pitr.yml -l pg-test -e '{"pg_pitr": {"cluster": "pg-meta"}}'
PITR Task Parameters
pg_pitr:# Define PITR taskcluster:"pg-meta"# Source cluster name (for restoring from another cluster's backup)type:default # Recovery target type: default, time, xid, name, lsn, immediatetime:"2025-01-01 10:00:00+00"# Recovery target: point in timename:"some_restore_point"# Recovery target: named restore pointxid:"100000"# Recovery target: transaction IDlsn:"0/3000000"# Recovery target: log sequence numberset:latest # Backup set to restore from, default: latesttimeline:latest # Target timeline, can be integer, default: latestexclusive:false# Exclude target point, default: falseaction:pause # Post-recovery action: pause, promote, shutdownarchive:true# Keep archive settings, default: true; set false for exploratory recoverybackup:false# Backup existing data to /pg/data-backup before restore? default: falsedb_include:[]# Include only these databasesdb_exclude:[]# Exclude these databaseslink_map:{}# Tablespace link mappingprocess:4# Parallel recovery processes, defaults to node_cpurepo:{}# Recovery source repo configurationdata:/pg/data # Recovery data directoryport:5432# Recovery instance listen port
Subtasks
This playbook contains the following subtasks:
# down : stop HA and shutdown patroni and postgres# - pause : pause patroni auto failover# - stop : stop patroni and postgres services# - stop_patroni : stop patroni service# - stop_postgres : stop postgres service## pitr : execute PITR recovery process# - config : generate pgbackrest config and recovery script# - backup : perform optional backup to original data# - restore : run pgbackrest restore command# - recovery : start postgres and complete recovery# - verify : verify recovered cluster control data## up : start postgres/patroni and restore HA# - etcd : clean etcd metadata before startup# - start : start patroni and postgres services# - start_postgres : start postgres service# - start_patroni : start patroni service# - resume : resume patroni auto failover
Recovery Target Types
Type
Description
Example
default
Recover to end of WAL archive stream (latest state)