Version v3.7.0 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.
Administration
How to maintain an existing PostgreSQL cluster with Pigsty?
Here are some SOP for common pgsql admin tasks
- Case 1: Create Cluster
- Case 2: Create User
- Case 3: Create Database
- Case 4: Reload Service
- Case 5: Reload HBARule
- Case 6: Config Cluster
- Case 7: Append Replica
- Case 8: Remove Replica
- Case 9: Remove Cluster
- Case 10: Switchover
- Case 11: Backup Cluster
- Case 12: Restore Cluster
- Case 13: Adding Packages
- Case 14: Install Extension
- Case 15: Minor Upgrade
- Case 16: Major Upgrade
Cheatsheet
PGSQL playbooks and shortcuts:
Patroni admin command and shortcuts:
pgBackRest backup & restore command and shortcuts:
Systemd components quick reference
Create Cluster
To create a new Postgres cluster, define it in the inventory first, then init with:
Beware, perform
bin/node-addfirst, thenbin/pgsql-add, PGSQL works on managed nodes only.
Create User
To create a new business user on the existing Postgres cluster, add user definition to all.children.<cls>.pg_users, then create the user as follows:
Create Database
To create a new database user on the existing Postgres cluster, add database definition to all.children.<cls>.pg_databases, then create the database as follows:
Note: If the database has specified an owner, the user should already exist, or you’ll have to Create User first.
Reload Service
Services are exposed access point served by HAProxy.
This task is used when cluster membership has changed, e.g., append/remove replicas, switchover/failover / exposing new service or updating existing service’s config (e.g., LB Weight)
To create new services or reload existing services on entire proxy cluster or specific instances:
Reload HBARule
This task is used when your Postgres/Pgbouncer HBA rules have changed, you may have to reload hba to apply changes.
If you have any role-specific HBA rules, you may have to reload hba after a switchover/failover, too.
To reload postgres & pgbouncer HBA rules on entire cluster or specific instances:
Config Cluster
To change the config of a existing Postgres cluster, you have to initiate control command on admin node with admin user:
Change patroni parameters & postgresql.parameters, save & apply changes with the wizard.
Append Replica
To add a new replica to the existing Postgres cluster, you have to add its definition to the inventory: all.children.<cls>.hosts, then:
It will add node <ip> to pigsty and init it as a replica of the cluster <cls>.
Cluster services will be reloaded to adopt the new member
Remove Replica
To remove a replica from the existing PostgreSQL cluster:
It will remove instance <ip> from cluster <cls>. Cluster services will be reloaded to kick the removed instance from load balancer.
Remove Cluster
To remove the entire Postgres cluster, just run:
Switchover
You can perform a PostgreSQL cluster switchover with patroni cmd.
Backup Cluster
To create a backup with pgBackRest, run as local dbsu:
Check Backup & PITR for details.
Restore Cluster
To restore a cluster to a previous time point (PITR), run as local dbsu:
Check Backup & Restore docs for more details.
Adding Packages
To add the newer version of RPM/DEB packages, you have to add them to repo_packages and repo_url_packages
Then rebuild repo on infra nodes with ./infra.yml -t repo_build subtask, Then you can install these packages with ansible module package:
Install Extension
If you want to install extension on pg clusters, Add them to pg_extensions and make sure them installed with:
Some extension needs to be loaded in shared_preload_libraries, You can add them to pg_libs, or Config an existing cluster.
Finally, CREATE EXTENSION <extname>; on the cluster primary instance to install it.
Check PGSQL Extensions: Install for details.
Minor Upgrade
To perform a minor server version upgrade/downgrade, you have to add packages to yum/apt repo first.
Then perform a rolling upgrade/downgrade from all replicas, then switchover the cluster to upgrade the leader.
Major Upgrade
The simplest way to achieve a major version upgrade is to create a new cluster with the new version, then migration with logical replication & green/blue deployment.
You can also perform an in-place major upgrade, which is not recommended, especially when certain extensions are installed. But it is possible.
Assume you want to upgrade PostgreSQL 14 to 15, you have to add packages to yum/apt repo, and guarantee the extensions have the exact same version too.
Tuning PostgreSQL parameters
Common system maintenance tasks
Common failures and troubleshooting strategies
Handling accidental deletion of data, tables, and databases