Database
In this document, “database” refers to a logical object within a database cluster created with
CREATE DATABASE.
A PostgreSQL cluster can serve multiple databases at the same time. Define the required databases in the Pigsty cluster configuration.
Pigsty customizes the template1 database with default schemas, extensions, and privileges.
New databases inherit these settings from template1.
Use template to select another template for instant database cloning.
By default, Pigsty adds each application database to the PgBouncer connection pool with a 1:1 mapping.
pg_exporter discovers application databases automatically to monitor their objects.
Pigsty also registers all databases as PostgreSQL data sources in Grafana on every INFRA node for PGCAT dashboards.
Define Database
Define application databases in pg_databases, an array of database definition objects.
During cluster initialization, Pigsty creates databases in definition order.
A later database can therefore use an earlier database as a template.
Example from Pigsty demo pg-meta cluster:
Each database definition has the fields below.
Only name is required:
From Pigsty
v4.1.0, usepool_reserveandpool_connlimitfor database pools. These unify the legacy aliasespool_size_reserve/pool_max_db_conn.
Parameter Overview
Only name is required.
It must be a valid database name that is unique within the cluster.
All other parameters have defaults.
Immutable parameters take effect only at creation.
To change them, recreate the database.
| Field | Category | Type | Attr | Description |
|---|---|---|---|---|
name | Basic | string | Required | Database name, must be valid and unique |
state | Basic | enum | Optional | State: create (default), absent, recreate |
owner | Basic | string | Mutable | Database owner, defaults to postgres |
comment | Basic | string | Mutable | Database comment |
template | Template | string | Immutable | Template database, default template1 |
strategy | Template | enum | Immutable | Clone strategy: FILE_COPY or WAL_LOG (PG15+) |
encoding | Encoding | string | Immutable | Character encoding, default inherited (UTF8) |
locale | Encoding | string | Immutable | Locale setting, default inherited (C) |
lc_collate | Encoding | string | Immutable | Collation rule, default inherited (C) |
lc_ctype | Encoding | string | Immutable | Character classification, default inherited (C) |
locale_provider | Encoding | enum | Immutable | Locale provider: libc, icu, builtin (PG15+) |
icu_locale | Encoding | string | Immutable | ICU locale rules (PG15+) |
icu_rules | Encoding | string | Immutable | ICU collation customization (PG16+) |
builtin_locale | Encoding | string | Immutable | Builtin locale rules (PG17+) |
tablespace | Storage | string | Mutable | Default tablespace, change triggers data migration |
is_template | Privilege | bool | Mutable | Mark as template database |
allowconn | Privilege | bool | Mutable | Allow connections, default true |
revokeconn | Privilege | bool | Mutable | Revoke PUBLIC CONNECT privilege |
connlimit | Privilege | int | Mutable | Connection limit, -1 for unlimited |
baseline | Init | string | Mutable | SQL baseline file path, runs on every provisioning |
schemas | Init | (string|object)[] | Mutable | Schema definitions to create |
extensions | Init | (string|object)[] | Mutable | Extension definitions to install |
parameters | Init | object | Mutable | Database-level parameters |
pgbouncer | Pool | bool | Mutable | Add to connection pool, default true |
pool_mode | Pool | enum | Mutable | Pool mode: transaction (default) |
pool_size | Pool | int | Mutable | Default pool size, default 50 |
pool_size_min | Pool | int | Mutable | Min pool size, default 0 |
pool_reserve | Pool | int | Mutable | Reserve pool size, default 30 |
pool_connlimit | Pool | int | Mutable | Max database connections, default 100 |
pool_auth_user | Pool | string | Mutable | Auth query user |
register_datasource | Monitor | bool | Mutable | Register to Grafana datasource, default true |
Parameter Details
name
String, required. Database name - must be unique within the cluster.
The current role does not enforce this regular expression.
It double-quotes SQL identifiers, but also uses the name in temporary paths and shell/SQL commands.
For safe operation throughout the automation, keep the name within 63 bytes and follow ^[A-Za-z_][A-Za-z0-9_$]{0,62}$.
Avoid spaces, quotes, slashes, and other special characters.
state
Enum for database operation: create, absent, or recreate. Default create.
| State | Description |
|---|---|
create | Default, create or modify database, adjust mutable params if exists |
absent | Delete database with DROP DATABASE WITH (FORCE) |
recreate | Drop then create, for database reset |
owner
String. Database owner, defaults to pg_dbsu (postgres) if not specified.
The target user must exist. To change the owner, Pigsty runs the following commands. The old owner retains existing privileges.
The database owner has full control, including schema, table, and extension creation. This is useful for multi-tenant deployments.
comment
String. Database comment, defaults to business database {name}.
Pigsty uses COMMENT ON DATABASE to set the comment.
Comments support Chinese and special characters; Pigsty escapes quotes automatically.
PostgreSQL stores them in pg_shdescription, the shared-object comment catalog.
Use \l+ to show them.
template
String, immutable. Template database for creation, default template1.
CREATE DATABASE clones the template, including objects, extensions, schemas, and permissions.
Pigsty customizes template1 during cluster initialization.
New databases inherit these settings.
| Template | Description |
|---|---|
template1 | Default, includes Pigsty pre-configured extensions/schemas/perms |
template0 | Clean template, required for non-default locale providers |
| Custom database | Use existing database as template for cloning |
For the icu or builtin locale provider, specify template: template0.
You cannot override the locale settings inherited from template1.
template0 omits the monitoring extensions, schemas, and default privileges.
Use it for a fully custom database.
strategy
Enum, immutable. Clone strategy: FILE_COPY or WAL_LOG. Available PG15+.
| Strategy | Description | Use Case |
|---|---|---|
FILE_COPY | Direct file copy with checkpoints before and after | Large templates, lower WAL volume |
WAL_LOG | Block-by-block copy written to WAL; PG15+ default | Small templates, non-blocking |
WAL_LOG permits template connections during cloning but is less efficient for large templates.
PG14 and earlier ignore this setting.
encoding
String, immutable. Character encoding, inherits from template if unspecified (usually UTF8).
We strongly recommend UTF8 unless you have special requirements.
You cannot change the encoding after creation.
locale
String, immutable. Locale setting - sets both lc_collate and lc_ctype. Inherits from template (usually C).
This setting controls string sort order and character classification.
Use C or POSIX for the best performance and cross-platform consistency.
For language-specific sorting, use a locale such as zh_CN.UTF-8.
lc_collate
String, immutable. String collation rule. Inherits from template (usually C).
This setting controls ORDER BY and comparison results.
Common values are C (byte order, fastest), C.UTF-8, en_US.UTF-8, and zh_CN.UTF-8.
You cannot change it after creation.
lc_ctype
String, immutable. Character classification rule for upper/lower case, digits, letters. Inherits from template (usually C).
This setting affects upper(), lower(), regex \w, and similar operations.
You cannot change it after creation.
locale_provider
Enum, immutable. Locale implementation provider: libc, icu, or builtin. Available PG15+, default libc.
| Provider | Version | Description |
|---|---|---|
libc | - | OS C library, traditional default, varies by system |
icu | PG15+ | ICU library, cross-platform consistent, more langs |
builtin | PG17+ | PostgreSQL builtin, most efficient, C/C.UTF-8 only |
For icu or builtin, use template: template0.
Also set the corresponding icu_locale or builtin_locale.
icu_locale
String, immutable. ICU locale identifier. Available PG15+ when locale_provider: icu.
ICU identifiers follow BCP 47. Common values:
| Value | Description |
|---|---|
en-US | US English |
en-GB | British English |
zh-Hans | Simplified Chinese |
zh-Hant | Traditional Chinese |
ja-JP | Japanese |
ko-KR | Korean |
icu_rules
String, immutable. Custom ICU collation rules. Available PG16+.
Use ICU Collation Customization to adjust the default sort behavior.
builtin_locale
String, immutable. Builtin locale provider rules. Available PG17+ when locale_provider: builtin. Values: C or C.UTF-8.
PG17 introduced the builtin provider.
It is faster than libc and provides consistent behavior across platforms.
It supports only C/C.UTF-8 collation.
tablespace
String, mutable. Default tablespace, default pg_default.
Changing the tablespace moves all database objects to the new tablespace. For large databases, this physical migration can take a long time. Use it carefully.
is_template
Boolean, mutable. Mark database as template, default false.
If true, any user with CREATEDB can clone this database as a template.
Template databases usually contain predefined schemas, extensions, and data.
Before it deletes an is_template: true database, Pigsty runs ALTER DATABASE ... IS_TEMPLATE false.
allowconn
Boolean, mutable. Allow connections, default true.
If false, the database rejects all connections, including superuser connections.
Use this setting for maintenance or archival.
revokeconn
Boolean, mutable. Revoke PUBLIC CONNECT privilege, default false.
When true, Pigsty executes:
- Revoke PUBLIC CONNECT, regular users can’t connect
- Grant connect to replication user (
replicator) and monitor user (dbuser_monitor) - Grant connect to admin user (
dbuser_dba) and owner withWITH GRANT OPTION
Setting false restores PUBLIC CONNECT privilege.
connlimit
Integer, mutable. Max concurrent connections, default -1 (unlimited).
A positive integer limits simultaneous sessions. The limit does not apply to superusers.
baseline
String. SQL baseline file path executed while provisioning the database.
Baseline files usually contain schema definitions, initial data, and stored procedures.
The path is relative to the Ansible search path, usually files/.
If baseline is defined, the role runs that file on every provisioning pass for the database.
It runs even if the database already exists, and after state: recreate.
Make the SQL idempotent, or do not rerun it against an existing database.
schemas
Array, mutable (add/remove). Schema definitions to create or drop. Elements can be strings or objects.
Simple format - strings for schema names (create only):
Full format - objects for owner and drop operations:
Creation uses IF NOT EXISTS.
Deletion uses CASCADE, which deletes all objects in the schema.
extensions
Array, mutable (add/remove). Extension definitions to install or uninstall. Elements can be strings or objects.
Simple format - strings for extension names (install only):
Full format - objects for schema, version, and uninstall:
Installation uses IF NOT EXISTS ... CASCADE.
If an extension exists, PostgreSQL reports a NOTICE and skips it.
When possible, PostgreSQL installs dependencies automatically.
Uninstallation uses CASCADE and deletes dependent objects.
parameters
Object, mutable. Database-level config params via ALTER DATABASE ... SET. Applies to all sessions connecting to this database.
Use special value DEFAULT (case-insensitive) to reset to PostgreSQL default:
pgbouncer
Boolean, mutable. Add database to Pgbouncer pool list, default true.
If false, PgBouncer excludes the database and clients cannot access it through the connection pool.
Use this for internal management databases or direct connections.
pool_mode
Enum, mutable. Pgbouncer pool mode: transaction, session, or statement. Default transaction.
| Mode | Description | Use Case |
|---|---|---|
transaction | Return connection after txn | Most OLTP apps, default |
session | Return connection after session | Apps needing session state |
statement | Return after each statement | Simple stateless queries |
pool_size
Integer, mutable. Pgbouncer default pool size, default 50.
The pool size limits regular backend connections for this database pool.
pool_size_min controls prewarmed connections.
Adjust these values for the workload.
pool_size_min
Integer, mutable. Pgbouncer minimum pool size, default 0.
A value > 0 creates that number of backend connections in advance. This reduces latency for the first request.
pool_reserve
Integer, mutable. Pgbouncer reserve pool size, default 30.
If the default pool is full, PgBouncer can add up to pool_reserve connections for increased traffic.
pool_connlimit
Integer, mutable. Max connections via Pgbouncer pool, default 100.
This PgBouncer limit is independent of the database connlimit parameter.
pool_auth_user
String, mutable. User for Pgbouncer auth query.
Enable pgbouncer_auth_query to use this field.
If set, all PgBouncer connections to this database use the specified user for auth-query password verification.
register_datasource
Boolean, mutable. Register database to Grafana as PostgreSQL datasource, default true.
Set false to skip Grafana registration.
Use this for temporary, test, or internal databases that do not need monitoring.
Template Inheritance
If you do not set them explicitly, many parameters inherit from the template database.
The default template is template1.
Cluster initialization parameters determine its encoding settings:
| Cluster Param | Default | Description |
|---|---|---|
pg_encoding | UTF8 | Cluster encoding |
pg_locale | C / C-UTF-8 (if supported) | Cluster locale |
pg_lc_collate | C / C-UTF-8 (if supported) | Cluster collation |
pg_lc_ctype | C / C-UTF-8 (if supported) | Cluster ctype |
Unless you select another template, new databases clone template1.
PG_PROVISION customizes it with extensions, schemas, and default privileges.
Deep Customization
Use these parameters to customize the template database:
pg_default_roles: Default predefined roles and system userspg_default_privileges: Default privileges for objects created by admin userpg_default_schemas: Default schemas to createpg_default_extensions: Default extensions to createpg_default_hba_rules: Default PostgreSQL HBA rulespgb_default_hba_rules: Default Pgbouncer HBA rules
If these parameters are insufficient, use pg_init to specify custom cluster initialization scripts:
pg-init: Cluster init scriptpg-init-template.sql: Template customization SQLpg-init-roles.sql: Default roles SQL
Locale Providers
PostgreSQL 15+ introduced locale_provider to select the locale implementation.
You cannot change it after database creation.
The Pigsty configure wizard selects the builtin C.UTF-8/C locale provider from the PostgreSQL and OS versions.
Databases inherit the cluster locale by default.
To select a different provider, use template0.
Using ICU provider (PG15+):
Using builtin provider (PG17+):
Provider comparison: libc (traditional, OS-dependent), icu (PG15+, cross-platform, feature-rich), builtin (PG17+, most efficient C/C.UTF-8).
Connection Pool
PgBouncer improves short-connection performance and reduces contention. It prevents excessive connections from overwhelming the database and provides flexibility during migrations.
Pigsty configures one connection pool for each PostgreSQL instance.
It runs as pg_dbsu, which defaults to the postgres OS user.
The pool connects to PostgreSQL through the /var/run/postgresql Unix socket.
Pigsty adds all databases in pg_databases to pgbouncer by default.
Set pgbouncer: false to exclude specific databases.
Pgbouncer database list and config params are defined in /etc/pgbouncer/database.txt:
When you create databases, Pigsty reloads the PgBouncer database list online. Existing connections remain active.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)