Sub topic 12
Inspecting who can do what
This chapter assumes you can already create roles, run GRANT and REVOKE, and use group roles and ALTER DEFAULT PRIVILEGES. You also need to be able to read psql output. It covers how to check the result of those commands.
Two different questions
"What can alice do?" can mean two things:
- Granted privileges are the entries stored in the access control list (ACL) of each object. You read them to see who was granted what, and by whom.
- Effective privileges are what a role can actually do right now. They include privileges inherited through group roles, rights that
PUBLICholds, and ownership. A superuser bypasses all checks.
Catalog queries and psql commands show granted privileges. The has_*_privilege functions compute effective privileges. Use both, because they answer different questions.
Listing roles
In psql, \du lists roles with their attributes and, in older versions, their memberships. The catalog view pg_roles gives the same data as a table you can filter:
SELECT rolname, rolcanlogin, rolsuper, rolcreatedb, rolcreaterole, rolinherit
FROM pg_roles
WHERE rolname NOT LIKE 'pg\_%'
ORDER BY rolname;The NOT LIKE 'pg\_%' filter hides the predefined roles such as pg_monitor. Use pg_roles, not the underlying pg_authid. pg_authid includes password hashes and only superusers can read it.
Membership lives in pg_auth_members. \drg in PostgreSQL 16 and later prints it. This query works everywhere, and the commented columns exist only in 16 and later:
SELECT r.rolname AS group_role,
m.rolname AS member,
g.rolname AS granted_by,
am.admin_option
-- PostgreSQL 16+: , am.inherit_option, am.set_option
FROM pg_auth_members am
JOIN pg_roles r ON r.oid = am.roleid
JOIN pg_roles m ON m.oid = am.member
JOIN pg_roles g ON g.oid = am.grantor
ORDER BY group_role, member;Reading privileges with psql
\dp (also spelled \z) shows the privileges on tables, views, and sequences. Pass a pattern to narrow it:
\dp shop.ordersThe output has an "Access privileges" column. Each entry has the form grantee=privileges/grantor:
app_owner=arwdDxt/app_owner
app_readonly=r/app_owner
app_writer=arw/app_ownerThe letters are abbreviations:
| Letter | Privilege | Letter | Privilege |
|---|---|---|---|
| `r` | SELECT | `x` | REFERENCES |
| `w` | UPDATE | `t` | TRIGGER |
| `a` | INSERT | `U` | USAGE |
| `d` | DELETE | `C` | CREATE |
| `D` | TRUNCATE | `c` | CONNECT |
| `X` | EXECUTE | `T` | TEMPORARY |
PostgreSQL 17 adds m for MAINTAIN. A * after a letter means the grantee also holds the grant option. An empty grantee, as in =r/app_owner, means PUBLIC. The column privileges are listed in a separate column of \dp.
A blank "Access privileges" column means the ACL is NULL. This is the built-in default: the owner has everything and nobody else has anything, except for object types where PUBLIC receives defaults (functions, databases, and types). A blank column and an empty-looking ACL are not the same as "nobody has access". The owner still does.
Related commands:
\lshows database privileges.\dn+shows schema privileges.\df+shows function privileges.\ddpshows default privileges set withALTER DEFAULT PRIVILEGES.
Reading ACLs with SQL
The aclexplode function turns an ACL array into rows, one per grantee and privilege. This is the way to script an audit of one object:
SELECT CASE WHEN a.grantee = 0 THEN 'PUBLIC'
ELSE a.grantee::regrole::text END AS grantee,
a.grantor::regrole AS grantor,
a.privilege_type,
a.is_grantable
FROM pg_class c
CROSS JOIN LATERAL aclexplode(c.relacl) AS a
WHERE c.oid = 'shop.orders'::regclass
ORDER BY grantee, a.privilege_type;A grantee OID of 0 stands for PUBLIC. If relacl is NULL, the query returns no rows, which is the default ACL case described above. Other catalogs have the same column under different names: nspacl in pg_namespace, datacl in pg_database, and proacl in pg_proc.
The standard information_schema views give a portable alternative:
SELECT grantee, privilege_type, is_grantable
FROM information_schema.role_table_grants
WHERE table_schema = 'shop' AND table_name = 'orders'
ORDER BY grantee, privilege_type;These views only list privileges where the grantor or grantee is a role the current session has enabled, plus PUBLIC. A limited role running this query sees an incomplete picture. For a full audit, use the system catalogs as a superuser or as a member of pg_read_all_data. Use information_schema.role_column_grants for column-level grants.
Asking about effective privileges
The has_*_privilege functions answer "could this role do this now?". The first argument is a role name or OID. If you omit it, the function checks the current user.
SELECT has_database_privilege('alice', 'appdb', 'CONNECT') AS can_connect,
has_schema_privilege('alice', 'shop', 'USAGE') AS can_use_schema,
has_table_privilege('alice', 'shop.orders', 'SELECT') AS can_select,
has_table_privilege('alice', 'shop.orders', 'UPDATE') AS can_update,
has_column_privilege('alice', 'shop.orders', 'customer', 'SELECT') AS can_read_customer,
has_any_column_privilege('alice', 'shop.orders', 'SELECT') AS can_read_some_column;There are matching functions for sequences, functions, and more: has_sequence_privilege, has_function_privilege, and pg_has_role for membership. A few behaviors matter in practice:
- The privilege argument is a keyword such as
'SELECT'. A comma-separated list returns true if the role holds any of them. AddWITH GRANT OPTIONto test for the grant option. has_table_privilegeis false for a role that has only column-levelSELECT.has_any_column_privilegeis true for that role. A table-level grant makeshas_column_privilegetrue for every column.- Because these functions take a role argument, you can build an access matrix for every login role in one query:
SELECT r.rolname,
has_table_privilege(r.oid, 'shop.orders', 'SELECT') AS can_select,
has_table_privilege(r.oid, 'shop.orders', 'INSERT') AS can_insert,
has_table_privilege(r.oid, 'shop.orders', 'UPDATE') AS can_update,
has_table_privilege(r.oid, 'shop.orders', 'DELETE') AS can_delete
FROM pg_roles r
WHERE r.rolcanlogin
ORDER BY r.rolname;Inheritance and SET ROLE
With an INHERIT membership, the functions count privileges from group roles. If alice is a member of app_readonly and the membership is NOINHERIT (or INHERIT FALSE in PostgreSQL 16 and later), has_table_privilege('alice', 'shop.orders', 'SELECT') returns false even though alice could switch roles and gain the access. To check that path, use pg_has_role:
SELECT pg_has_role('alice', 'app_readonly', 'USAGE'); -- inherits its privileges
SELECT pg_has_role('alice', 'app_readonly', 'MEMBER'); -- is a member, directly or indirectlyUSAGE means the role's privileges are available without switching. MEMBER means alice is a member either way. PostgreSQL 16 adds a 'SET' check for whether SET ROLE is allowed. When a role's access is not what you expect, check these in order: its direct ACL entries, its memberships, whether each membership inherits, and then PUBLIC.
Auditing future objects
Default privileges are stored in pg_default_acl. \ddp prints them, and this query is the scriptable version:
SELECT defaclrole::regrole AS for_role,
defaclnamespace::regnamespace AS in_schema,
defaclobjtype AS object_type,
defaclacl AS acl
FROM pg_default_acl
ORDER BY for_role, in_schema;The object type is r for tables, S for sequences, f for functions, T for types, and n for schemas. Each entry applies only to objects created by for_role. If a new table has the wrong privileges, check that the role that created it matches the one named in FOR ROLE.
A good audit compares both views. Check that existing objects have the ACLs you expect, and that pg_default_acl will produce the same ACLs for new objects.
Exercise
alice has only GRANT SELECT (id, customer) ON shop.orders TO alice. What do has_table_privilege('alice', 'shop.orders', 'SELECT'), has_column_privilege('alice', 'shop.orders', 'customer', 'SELECT'), and has_any_column_privilege('alice', 'shop.orders', 'SELECT') return?
Show answer
The first returns false, because alice has no table-level SELECT. The second returns true, because she holds SELECT on the customer column. The third returns true, because she holds SELECT on at least one column.
Exercise
A table's "Access privileges" column in \dp shows app_owner=arwdDxt/app_owner and =r/app_owner. Who can read the table, and which role granted the second entry?
Show answer
The owner appowner can read it through the first entry. The second entry has an empty grantee, so it applies to PUBLIC, which means every role can read the table. It was granted by appowner.