Managing PostgreSQL Databases
Quick Start
Pigsty manages databases declaratively.
First define databases in the inventory.
Then run bin/pgsql-db <cls> <dbname> to create or modify them.
For the complete database definition reference, see Database Configuration. For database access permissions, see Access Control: Database Isolation.
You can set some parameters only at creation time.
To change these parameters, recreate the database with state: recreate.
| Action | Command | Description |
|---|---|---|
| Create Database | bin/pgsql-db <cls> <db> | Create new business database |
| Modify Database | bin/pgsql-db <cls> <db> | Modify existing database properties |
| Delete Database | bin/pgsql-db <cls> <db> | Delete database (requires state: absent) |
| Rebuild Database | bin/pgsql-db <cls> <db> | Drop and recreate (requires state: recreate) |
| Clone Database | bin/pgsql-db <cls> <db> | Clone database using template |
Create Database
During PostgreSQL cluster creation, the pg_db task automatically creates databases defined in pg_databases.
To create a database on an existing cluster, add its definition to all.children.<cls>.pg_databases.
Then run:
Example: Create business database myapp
Result: Pigsty creates myapp on the primary with dbuser_myapp as its owner.
It creates the app schema and enables the pg_trgm and btree_gin extensions.
It also adds the database to the Pgbouncer pool and registers a Grafana data source.
For manual database creation, you must ensure Pgbouncer pool and Grafana datasource sync yourself.
Modify Database
Use the same command as for creation.
The playbook is idempotent when no baseline SQL is defined.
If the target database exists, Pigsty changes its properties to match the configuration. However, you can set some properties only at creation.
Immutable properties: To change these properties after creation, use state: recreate:
name(database name),template,strategy(clone strategy)encoding,locale/lc_collate/lc_ctype,locale_provider/icu_locale/icu_rules/builtin_locale
All other properties can be modified. Common examples:
Modify owner: Change the owner field.
Pigsty runs ALTER DATABASE ... OWNER TO and grants permissions.
Modify connection limit: Use connlimit to limit max connections.
Revoke public connect: Set revokeconn: true to revoke the PUBLIC CONNECT privilege.
Only the owner, DBA, monitor, and replication users can then connect.
Manage parameters: Use the parameters dictionary to set database-level parameters.
Pigsty generates ALTER DATABASE ... SET commands.
Use the special value DEFAULT to reset a parameter.
Manage schemas: Use the schemas array with simple or extended definitions.
To drop a schema with CASCADE, set state: absent.
Manage extensions: Use the extensions array with simple or extended definitions.
To uninstall an extension with CASCADE, set state: absent.
Schema deletion and extension removal use CASCADE.
This deletes all dependent objects.
Understand the effect before you run these operations.
Connection pool config: By default, Pigsty adds all databases to Pgbouncer.
Configure pooling with pgbouncer, pool_mode, pool_size, pool_reserve, pool_size_min, pool_connlimit, and pool_auth_user.
Since Pigsty
v4.1.0, usepool_reserveandpool_connlimitfor database pool fields. These names replace the legacy aliasespool_size_reserveandpool_max_db_conn.
Delete Database
To delete a database, set state to absent and execute:
Config example:
Deletion process: If is_template: true, Pigsty first runs ALTER DATABASE ... IS_TEMPLATE false.
It then uses DROP DATABASE ... WITH (FORCE) (PG13+) to delete the database and terminate all its connections.
It removes the database from the Pgbouncer pool and unregisters its Grafana data source.
Protection: Pigsty cannot delete the system databases postgres, template0, and template1.
Deletion runs only on the primary.
Streaming replication sends the change to replicas.
Database deletion is irreversible and permanently deletes all data. Before deletion, verify that a recent backup exists. Confirm that no business uses the database, and notify stakeholders. Pigsty is not responsible for any data loss from database deletion. Use at your own risk.
Rebuild Database
recreate state rebuilds database (drop then create):
Config example:
Use cases: Test environment reset, clear dev database, modify immutable properties (encoding, locale), restore to initial state.
Difference from manual DROP + CREATE: One command performs the operation.
It preserves Pgbouncer and Grafana configuration automatically.
It also loads the baseline initialization script automatically.
Clone Database
Use the PostgreSQL template mechanism to clone a database. The template database must have no active connections during cloning.
Config example:
Instant Clone (PG18+): For PostgreSQL 18+, Pigsty configures file_copy_method by default.
With strategy: FILE_COPY, cloning takes about 200ms and does not copy data files.
For example, a 30GB database takes 18s with normal cloning and 200ms with instant cloning.
Manual clone: Ensure all connections to template are terminated:
Limitations: Instant cloning requires a supported filesystem: xfs, btrfs, zfs, or apfs.
Do not use the postgres database as a template.
In high-concurrency environments, clear all template connections within the cloning window of about 200ms.
Connection Pool Management
Connection pool params in database definitions are applied to Pgbouncer when creating/modifying databases.
By default, Pigsty adds all databases to the Pgbouncer pool (pgbouncer: true).
It adds their definitions to /etc/pgbouncer/database.txt.
This file also holds database-level pool parameters: pool_auth_user, pool_mode, pool_size, pool_reserve, pool_size_min, and pool_connlimit.
As the postgres operating system user, use the pgb alias to access the Pgbouncer administration database.
For more operations, see Pgbouncer Management.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)