Database
CREATE DATABASE within a database cluster.In this context, Database refers to the logical object created using the SQL command
CREATE DATABASEwithin a database cluster.
A PostgreSQL server can serve multiple databases at the same time. In Pigsty, define the required databases in the cluster configuration.
Pigsty customizes the default template database, template1.
It creates default schemas, installs default extensions, and configures default privileges.
New databases inherit these settings from template1 by default.
By default, Pigsty adds each business database to the Pgbouncer connection pool with a 1:1 mapping.
pg_exporter automatically discovers all business databases and monitors their objects.
Define Database
Define business databases in the cluster parameter pg_databases.
This parameter is an array of database definition objects.
Pigsty creates databases in definition order.
Thus, a later database can use an earlier database as its template.
Below is the database definition for the default pg-meta cluster in the Pigsty demo environment:
Each database definition is an object.
The following meta definition shows the available fields:
The only required field is name.
It should be a valid database name that is unique within the PostgreSQL cluster.
Other parameters have defaults.
name: Database name, required.baseline: SQL file path (Ansible search path, usually infiles), used to initialize database content.owner: Database owner, default ispostgrestemplate: Template used when creating the database, default istemplate1encoding: Database default character encoding, default isUTF8, default is consistent with the instance. It is recommended not to configure and modify.locale: Database default locale, default isC, it is recommended not to configure, keep consistent with the instance.lc_collate: Database default locale string collation, default is same as instance setting, it is recommended not to modify, must be consistent with template database. It is strongly recommended not to configure, or configure toC.lc_ctype: Database default LOCALE, default is same as instance setting, it is recommended not to modify or set, must be consistent with template database. It is recommended to configure to C oren_US.UTF8.allowconn: Whether to allow connection to the database, default istrue, not recommended to modify.revokeconn: Whether to revoke connection privilege to the database? Default isfalse. Iftrue,PUBLIC CONNECTprivilege on the database will be revoked. Only default users (dbsu|monitor|admin|replicator|owner) can connect. In addition,admin|ownerwill have GRANT OPTION, can grant connection privileges to other users.tablespace: Tablespace associated with the database, default ispg_default.connlimit: Database connection limit, default is-1, meaning no limit.extensions: Object array, each object defines an extension in the database, and the schema in which it is installed.parameters: KV object, each KV defines a parameter that needs to be modified for the database throughALTER DATABASE.pgbouncer: Boolean option, whether to add this database to Pgbouncer. All databases will be added to Pgbouncer list unless explicitly specified aspgbouncer: false.comment: Database comment information.pool_auth_user: Whenpgbouncer_auth_queryis enabled, all connections to this pgbouncer database will use the user specified here to execute authentication queries. You need to use a user with access to thepg_shadowtable.pool_mode: Database level pgbouncer pool mode, default is transaction, i.e., transaction pooling. If left empty, will usepgbouncer_poolmodeparameter as default value.pool_size: Database-level Pgbouncer default pool size, default50.pool_reserve: Database-level Pgbouncer reserve pool, default30; when the regular pool is exhausted, at most this many burst connections can be added.pool_size_min: Database level pgbouncer pool size min, default is 0pool_connlimit: Database level pgbouncer connection pool max database connections, default is 100
By default, new databases use template1 as their template.
During PG_PROVISION, Pigsty configures this template with extensions, schemas, and default privileges.
New databases inherit this configuration unless you explicitly select another template.
For database access privileges, see Access Control: Database Isolation.
Create Database
Pigsty creates databases defined in pg_databases during cluster initialization.
To create a database on an existing cluster, use bin/pgsql-db.
Add the definition to all.children.<cls>.pg_databases, then run:
Here are some considerations when creating a new database:
The database creation playbook is idempotent by default.
However, a baseline script can change this behavior.
Do not usually repeat the playbook on an existing database unless you know that its baseline SQL is also idempotent.
Manual database creation is not recommended, especially with the default Pgbouncer connection pool.
If you create databases manually, you must maintain the Pgbouncer database list and keep it consistent with PostgreSQL.
The pgsql-db tool and pgsql-db.yml playbook also add new databases to the Pgbouncer database list.
If the database owner differs from the default dbsu, postgres, verify that the owner user exists first.
Always create users before you create databases.
Pgbouncer Database
By default, Pigsty enables one Pgbouncer connection pool for each PostgreSQL instance.
The pool connects through the /var/run/postgresql Unix socket.
Connection pools can improve performance for short connections and reduce concurrency contention. They prevent excessive connections from overloading the database. They also provide more options during database migration.
By default, Pigsty adds all databases from pg_databases to the Pgbouncer database list.
To exclude a database, set pgbouncer: false in its definition.
The Pgbouncer database list is in /etc/pgbouncer/database.txt.
This file includes pool parameters from each database definition:
When you create databases, Pigsty refreshes the Pgbouncer database list. An online configuration reload applies the changes, normally without affecting existing connections.
Pgbouncer runs as the same dbsu as PostgreSQL.
The default is the postgres operating system user.
As dbsu, use the pgb alias for Pgbouncer administration.
To route a managed Pgbouncer database to another node, edit /etc/pgbouncer/database.txt.
Then reload the configuration and rebuild existing server connections:
The current
pgb-routehelper edits only/etc/pgbouncer/pgbouncer.ini. Pigsty stores managed database routes in the includeddatabase.txtfile. Thus, this helper does not change managed routes.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)