Sub topic 12
Inspecting Who Can Do What
This chapter assumes basic SQL, a working PostgreSQL connection through psql or another client, and the difference between a database, schema, and table. The goal is to inspect effective access before changing it.
A practical inspection workflow
Privilege inspection answers three different questions:
- Which roles exist, and what attributes do they have?
- Which roles are granted privileges on an object?
- Can a particular role actually perform an operation?
The last question matters because ownership, membership, inheritance, PUBLIC, and superuser status can affect the result.
| Question | Useful tool |
|---|---|
| What roles exist? | `\du` or `pg_roles` |
| Who has table privileges? | `\dp` or `information_schema` |
| What can this role do? | `has_*_privilege` functions |
| What future grants exist? | `\ddp` |
Inspect roles and membership
In psql, use these commands:
\du
\du aliceThe output shows attributes such as Login, Superuser, Create role, and Create DB. It also shows direct role memberships, but membership behavior depends on inheritance and SET ROLE.
For a queryable report, use the pg_roles catalog view:
SELECT rolname,
rolcanlogin,
rolinherit,
rolsuper,
rolcreatedb,
rolcreaterole
FROM pg_roles
WHERE rolname NOT LIKE 'pg\_%'
ORDER BY rolname;pg_roles includes sensitive role properties, so restrict access to administrative reports when appropriate.
Inspect object privileges
The \dp command lists table, view, sequence, and foreign-table access in psql:
\dp shop.ordersThe Access privileges column contains compact entries such as app_readonly=r. The letter r means SELECT. Other letters represent privileges such as INSERT, UPDATE, and DELETE. An entry ending in * indicates that the privilege was granted with grant option.
For a relational result, query information_schema:
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;This view focuses on privileges granted to roles that the current user can see. It does not present every reason access might succeed, such as ownership or superuser status.
Test effective access
Privilege-checking functions answer whether a named role can perform an operation:
SELECT has_table_privilege(
'alice',
'shop.orders',
'SELECT'
) AS can_select;
SELECT has_schema_privilege(
'alice',
'shop',
'USAGE'
) AS can_use_schema;These functions return Boolean values. They include privileges inherited through role membership and privileges granted to PUBLIC. A successful table check does not guarantee that a query will work if the role lacks USAGE on the containing schema.
You can also test column access:
SELECT has_column_privilege(
'alice',
'shop.orders',
'total',
'SELECT'
) AS can_read_total;Inspect default privileges
Default privileges control grants on objects created later. They do not change existing objects. In psql, inspect them with:
\ddpFor reliable auditing, check both current object privileges and default privileges. A correct report should also record the role that creates future objects, because default privileges belong to the object-creating role.
Exercise
Alice passes has_table_privilege('alice', 'shop.orders', 'SELECT'), but her query still fails with a permission error. What should you inspect next?
Show answer
Check whether Alice has USAGE on the shop schema, whether the object name resolves to the expected table, and whether row-level security or a view adds another restriction.