Sub topic 13
Predefined roles
PostgreSQL ships with a set of roles whose names start with pg_. Each one bundles a set of privileges that would otherwise take many GRANT statements, or that can't be expressed as a GRANT at all. This chapter assumes you know how GRANT, role membership, and inheritance work from earlier chapters.
What predefined roles are
A predefined role is an ordinary role that already exists in every cluster. You use it by granting it to another role with GRANT, the same way you would grant a group role.
- You can't drop them or alter their definition.
- The
pg_prefix is reserved, soCREATE ROLE pg_somethingfails. - They are
NOLOGINroles, so nobody connects as one. - A role that inherits from them gets their powers, subject to the membership rules from the inheritance chapter.
To list them:
SELECT rolname FROM pg_roles WHERE rolname LIKE 'pg\_%' ORDER BY rolname;The exact list depends on your server version, so check the release notes for yours.
The roles worth knowing
| Role | What it allows | Since |
|---|---|---|
| `pg_read_all_data` | `SELECT` on all tables, views, and sequences, and `USAGE` on all schemas | 14 |
| `pg_write_all_data` | `INSERT`, `UPDATE`, `DELETE` on all tables, views, and sequences, and `USAGE` on all schemas | 14 |
| `pg_monitor` | Membership in `pg_read_all_settings`, `pg_read_all_stats`, and `pg_stat_scan_tables` | 10 |
| `pg_read_all_settings` | Read every configuration parameter, including those meant for superusers | 10 |
| `pg_read_all_stats` | Read all `pg_stat_*` views and statistics extensions, including other sessions' query text | 10 |
| `pg_stat_scan_tables` | Run monitoring functions that take heavy locks on tables | 10 |
| `pg_signal_backend` | Cancel queries or terminate sessions of non-superuser roles | 9.6 |
| `pg_checkpoint` | Run `CHECKPOINT` | 15 |
| `pg_maintain` | `VACUUM`, `ANALYZE`, `CLUSTER`, `REINDEX`, `REFRESH MATERIALIZED VIEW`, and `LOCK` on all relations | 17 |
| `pg_database_owner` | Implicit membership for the current database's owner | 14 |
| `pg_read_server_files`, `pg_write_server_files`, `pg_execute_server_program` | Server-side file and program access through `COPY` and similar features | 11 |
pg_monitor is the convenient bundle. Granting it gives the member the three monitoring roles at once.
Read access to everything with pg_read_all_data
pg_read_all_data suits reporting users, BI tools, and analysts who need to read every table. It also covers tables created in the future, so it avoids the ALL TABLES limitation and the need for default privileges.
CREATE ROLE reporting LOGIN PASSWORD 'change-me';
GRANT pg_read_all_data TO reporting;The role grants table-level access but not the right to connect. If you revoked CONNECT from PUBLIC earlier, the new role still needs it:
GRANT CONNECT ON DATABASE appdb TO reporting;Verify the result from the privilege functions and the membership catalog:
SELECT pg_has_role('reporting', 'pg_read_all_data', 'USAGE') AS has_role,
has_table_privilege('reporting', 'shop.orders', 'SELECT') AS can_select;
SELECT roleid::regrole AS granted_role, member::regrole AS member, admin_option
FROM pg_auth_members
WHERE member = 'reporting'::regrole;Both checks return true for reporting. has_table_privilege reports the access that comes through the predefined role, because the server's access check includes it.
Write access with pg_write_all_data
pg_write_all_data grants INSERT, UPDATE, and DELETE everywhere. It doesn't grant SELECT. An UPDATE ... WHERE needs to read the columns in the WHERE clause, so a writer usually needs both roles:
CREATE ROLE etl LOGIN PASSWORD 'change-me';
GRANT pg_read_all_data, pg_write_all_data TO etl;
GRANT CONNECT ON DATABASE appdb TO etl;Neither data role grants CREATE on schemas, TRUNCATE, REFERENCES, TRIGGER, or EXECUTE on functions. They also don't let the member change table structure. Each of those needs an ordinary grant or ownership.
Monitoring with pg_monitor
Without special rights, pg_stat_activity hides the query text and some other columns of sessions owned by other roles. A monitoring role gets the full view through pg_monitor:
CREATE ROLE monitor LOGIN PASSWORD 'change-me';
GRANT pg_monitor TO monitor;
GRANT CONNECT ON DATABASE appdb TO monitor;Then, connected as monitor:
SELECT pid, usename, state, wait_event_type, left(query, 80) AS query
FROM pg_stat_activity
WHERE datname = 'appdb' AND pid <> pg_backend_pid();pg_monitor doesn't let the role read table data. It exposes query text, though, and that text can contain literal values such as email addresses or tokens. Treat the monitoring role as sensitive.
Limits of pg_read_all_data and pg_write_all_data
Row-level security still applies
The data roles don't carry the BYPASSRLS attribute. Suppose you later enable row-level security on shop.orders with the policy own_orders, which targets only app_readonly:
ALTER TABLE shop.orders ENABLE ROW LEVEL SECURITY;Then reporting sees zero rows. RLS denies by default when no policy applies to a role. Fix this with a policy for the role or, if it really should see everything, with the attribute:
CREATE POLICY reporting_all ON shop.orders
FOR SELECT
TO reporting
USING (true);Scope is the whole database
The role covers every schema, including ones created after the grant. That is convenient and also its risk: a table with payroll data created next year is readable at once. If you need only the shop schema, use explicit schema grants and default privileges instead.
Inheritance and SET ROLE
In PostgreSQL 16 and later, the INHERIT and SET options of the membership control how the member gets the role's powers. A member granted with INHERIT FALSE doesn't get the privileges automatically. It would have to run SET ROLE pg_read_all_data first, which is allowed only if SET is true. Before 16, inheritance is controlled by the member's own INHERIT attribute. The default in every version is that the powers are inherited.
Dangerous roles
Some predefined roles give much more than their names suggest.
pg_read_server_files,pg_write_server_files, andpg_execute_server_programlet a member runCOPY ... FROM/TOagainst server files and programs. These run as the operating system user that runs PostgreSQL. A member can read configuration files or overwrite files the database user owns.pg_execute_server_programcan run arbitrary commands. Treat them as close to superuser.pg_signal_backendlets a member terminate sessions of ordinary roles. A runaway script holding that role can disrupt the whole application.pg_checkpointandpg_maintainare low risk but still cause I/O load or locks when used carelessly.
The principle of least privilege applies here too. Give a predefined role only to a role that has a documented need, and review pg_auth_members for grants of the server-file and program roles regularly.
Choosing between predefined roles and explicit grants
| Situation | Better choice |
|---|---|
| Analyst or BI tool that must read everything | `pg_read_all_data` |
| Application read-only role limited to one schema | Explicit grants plus `ALTER DEFAULT PRIVILEGES` |
| Monitoring agent | `pg_monitor` |
| Application that writes to a few tables | Explicit `GRANT` on those tables |
| Tool that must import files from the server | Avoid `pg_read_server_files`; use client-side `\copy` |
To remove a grant, use REVOKE:
REVOKE pg_read_all_data FROM reporting;Exercise
A reporting role was granted pgreadall_data on a server where CONNECT was revoked from PUBLIC. It can't log in to the database. What is missing, and how do you fix it?
Show answer
pgreadall_data grants table and schema access, not the right to connect to the database. Run GRANT CONNECT ON DATABASE appdb TO reporting; (or grant CONNECT to a group role it belongs to).
Exercise
You enabled row-level security on shop.orders with a policy for appreadonly only. The reporting role holds pgreadalldata but sees no rows. Why, and what are two ways to fix it?
Show answer
Predefined data roles don't bypass row-level security, and with RLS enabled a role with no applicable policy gets no rows. Either create a policy for reporting, such as CREATE POLICY reporting_all ON shop.orders FOR SELECT TO reporting USING (true);, or give the role the BYPASSRLS attribute with ALTER ROLE reporting BYPASSRLS;.
Exercise
You query informationschema.roletable_grants for the reporting role and get no rows, yet reporting can read shop.orders. How do you confirm where its access comes from?
Show answer
Query pgauthmembers (for example SELECT roleid::regrole, member::regrole FROM pgauthmembers WHERE member = 'reporting'::regrole;) to see the predefined role grant, and use hastableprivilege('reporting', 'shop.orders', 'SELECT') or pghasrole to check effective access. Privileges inherited from predefined roles are not stored in the table's ACL.