User / Role
CREATE USER/ROLE.In this context, users refer to logical objects within a database cluster created using the SQL commands
CREATE USER/ROLE.
In PostgreSQL, users belong to the database cluster, rather than a specific database. Create business users before you create business databases.
Defining Users
Pigsty defines roles and users in database clusters through two configuration parameters:
pg_default_roles: Defines globally unified roles and userspg_users: Defines business users and roles at the database cluster level
The first parameter defines roles and users shared across the environment. The second defines business roles and users for individual clusters. Both use arrays of user definition objects.
You can define multiple users and roles. Pigsty creates global objects first, then cluster-level objects, following array order within each group. Thus, later users can belong to roles defined earlier.
Here is the business user definition for the default cluster pg-meta in the Pigsty demo environment:
Each user/role definition is an object that may include the following fields. Using dbuser_meta as an example:
- The only required field is
name. Use a valid username that is unique within the PostgreSQL cluster. - Roles do not need a
password. Business users with login access usually need one. passwordaccepts plaintext, scram-sha-256, or md5 hash strings. Avoid plaintext passwords.- Pigsty creates users and roles in array order. Define roles and groups before their members.
login,superuser,createdb,createrole,inherit,replication, andbypassrlsare Boolean flags.pgbounceris disabled by default. To add business users to the pool user list, explicitly set it totrue.
ACL System
Pigsty provides a built-in access control / ACL model. Assign its default business roles to users as required:
dbrole_readwrite: Role with global read-write access (production accounts primarily used by business should have database read-write privileges)dbrole_readonly: Role with global read-only access (if other businesses need read-only access, use this role)dbrole_admin: Role with DDL privileges (business administrators, scenarios requiring table creation in applications)dbrole_offline: Independent read-only role for ad hoc queries, ETL, and analytics; restrict its instance scope explicitly through HBA
To define your own ACL system, consider these parameters and templates:
pg_default_roles: System-wide roles and global userspg_default_privileges: Default privileges for newly created objectsroles/pgsql/templates/pg-init-roles.sql: Role creation SQL templateroles/pgsql/templates/pg-init-template.sql: Privilege SQL template
Creating Users
During the cluster initialization PROVISION phase, Pigsty creates users and roles one at a time.
It uses the definitions in pg_default_roles and pg_users.
To create users on an existing cluster, use bin/pgsql-user.
Add the definition to all.children.<cls>.pg_users, then run:
The user creation playbook is always idempotent, unlike the database creation playbook. If the target user exists, Pigsty changes its attributes to match the configuration. Thus, you can usually repeat the playbook on existing clusters.
Manual business user creation is not recommended, especially with the default Pgbouncer connection pool.
If you create users manually, you must maintain the Pgbouncer user list and keep it consistent with PostgreSQL.
The bin/pgsql-user tool and pgsql-user.yml playbook also add users to the Pgbouncer user list.
Modifying Users
The method for modifying PostgreSQL user attributes is the same as Creating Users.
First, change the required attributes in the user definition. Then run this command to apply them:
User modification changes attributes with ALTER USER; it does not delete users.
It also keeps existing privileges and groups.
It uses GRANT to add new roles.
Pgbouncer Users
Pigsty enables Pgbouncer connection pooling and manages its users by default.
Pigsty adds all users in pg_users that explicitly have the pgbouncer: true flag to the pgbouncer user list.
Users in the Pgbouncer connection pool are listed in /etc/pgbouncer/userlist.txt:
User-level connection pool parameters are maintained in a separate file: /etc/pgbouncer/useropts.txt, for example:
When you create a database, Pigsty refreshes the Pgbouncer database list. An online reload applies the changes 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.
When you create users, Pigsty refreshes userlist.txt and useropts.txt automatically.
An online reload applies these pool user changes, normally without affecting existing connections.
Use pgbouncer_auth_query to authenticate pool users with dynamic queries.
This is an alternative when you do not want to manage pool users directly.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)