Sub topic 13
Predefined roles
Assume you can connect to PostgreSQL, create roles, and use basic GRANT statements. This chapter focuses on PostgreSQL’s predefined roles, which provide broad, built-in access for common tasks without granting superuser status.
Why predefined roles exist
A predefined role is a role that PostgreSQL creates and maintains for you. Its membership represents a bundle of privileges that would otherwise require many individual grants.
Two useful examples are:
pg_read_all_datagives read access to all tables, views, and sequences, plusUSAGEon all schemas.pg_write_all_datagivesINSERT,UPDATE, andDELETEaccess to all tables, views, and sequences, plusUSAGEon all schemas.pg_monitorgives access to many monitoring views and functions. It includes the abilities provided bypg_read_all_settings,pg_read_all_stats, andpg_stat_scan_tables.
These roles are not superusers. They do not automatically allow a member to create databases, alter roles, or bypass every security feature.
Granting a predefined role
The following example creates separate reporting and monitoring accounts:
CREATE ROLE reporting LOGIN;
GRANT pg_read_all_data TO reporting;
CREATE ROLE monitor LOGIN;
GRANT pg_monitor TO monitor;reporting can read application data across the database. monitor can inspect server and database activity needed by many observability tools.
Membership can be controlled like membership in any other role. For example, an administrator can remove broad read access:
REVOKE pg_read_all_data FROM reporting;You can also grant a predefined role to a non-login group role, then make several accounts members of that group:
CREATE ROLE analysts NOLOGIN;
GRANT pg_read_all_data TO analysts;
GRANT analysts TO alice;
GRANT analysts TO bob;This keeps account management separate from the access policy.
Scope and limits
Predefined roles are intentionally broad. pg_read_all_data usually gives more access than a reporting account needs. If an analyst should read only sales.orders, a purpose-built group role with schema and table grants is safer.
These roles also do not replace row-level security (RLS). Row-level security is a policy system that filters which rows a role can access. The predefined data-access roles do not bypass RLS. A table owner, superuser, or role with the BYPASSRLS attribute can behave differently.
Check the target PostgreSQL version before using a predefined role. pg_read_all_data and pg_write_all_data are available in PostgreSQL 14 and later. pg_monitor is available in earlier supported versions, but its exact bundled capabilities depend on the server version.
Choosing between predefined and custom roles
Use pg_monitor for monitoring accounts, pg_read_all_data for genuinely database-wide readers, and custom roles for application access that should follow least privilege.
Exercise
Which predefined role fits a read-only monitoring account, and why is it different from pgreadall_data?
Show answer
pgmonitor fits because it grants access to monitoring views and functions. pgreadalldata focuses on reading application tables, views, and sequences.