Sub topic 9
Inspecting Roles and Privileges
This chapter assumes you know basic SQL, can use a terminal, and have access to a PostgreSQL server. You should also know that a table can have a primary key and foreign keys. The examples use the database mydb and the schema public.
Roles and memberships
A PostgreSQL role is a cluster-wide identity. A role can represent a person, an application, or a group. A role with the LOGIN attribute can connect to the server. A role without LOGIN is often used as a group that other roles inherit permissions from.
Privileges are permissions on database objects. Examples include CONNECT on a database, USAGE on a schema, and SELECT, INSERT, UPDATE, or DELETE on a table.
A connection has two useful identities:
session_useris the role that originally connected.current_useris the role PostgreSQL currently uses for permission checks. It can change afterSET ROLE.
A role can receive privileges directly or through membership in another role.
flowchart TD
A[Login role: reporting_app] -->|member of| B[Group role: reporting_readers]
B -->|has SELECT| C[Table: public.orders]
A -->|connects to| D[Database: mydb]
A -->|needs USAGE| E[Schema: public]caption: How a login role receives access through a group role alt: A login role connects to a database and uses schema access plus membership in a group role to select from a table
Inspecting roles
In psql, use \du to list roles and their attributes:
\duThe output shows attributes such as Login, Superuser, Create role, and Create DB. A superuser bypasses most permission checks, so do not use a superuser to test ordinary application access.
To inspect one role with SQL, query pg_roles. This catalog view hides password values while exposing useful attributes.
SELECT rolname,
rolcanlogin,
rolsuper,
rolcreatedb,
rolcreaterole,
rolinherit
FROM pg_roles
ORDER BY rolname;Role membership is stored in pg_auth_members. Join it to pg_roles to show member and group names.
SELECT member.rolname AS member_role,
group_role.rolname AS group_role
FROM pg_auth_members m
JOIN pg_roles member ON member.oid = m.member
JOIN pg_roles group_role ON group_role.oid = m.roleid
ORDER BY member_role, group_role;Inspecting object privileges
Database privileges appear with:
\l+ mydbSchema privileges appear with:
\dn+ publicTable, view, sequence, and schema access control lists appear with:
\dp public.*The alias \z public.* shows the same table privilege information. In an access control list, an entry such as r means SELECT, a means INSERT, and w means UPDATE.
You can inspect privileges through standard views as well:
SELECT grantee,
table_schema,
table_name,
privilege_type,
is_grantable
FROM information_schema.role_table_grants
WHERE table_schema = 'public'
AND table_name = 'orders'
ORDER BY grantee, privilege_type;information_schema presents portable information, but it may omit privileges that the current role cannot see. PostgreSQL catalogs and functions provide more detail.
SELECT current_user,
session_user,
has_schema_privilege(current_user, 'public', 'USAGE') AS can_use_schema,
has_table_privilege(current_user, 'public.orders', 'SELECT') AS can_select_orders;A successful SELECT also requires access to the containing database and schema. These checks therefore help identify which permission layer is missing.
Common errors and safe checks
A role can have SELECT on a table but still fail because it lacks CONNECT on the database or USAGE on the schema. A role can also select existing rows but fail to insert into a table because it lacks sequence privileges for an identity or serial column.
Use a test session with the intended login role when possible. Avoid changing privileges while investigating. Ask a database administrator before granting access, and never place passwords in SQL scripts or command history.
| Object | Common privilege | What it permits |
|---|---|---|
| Database | `CONNECT` | Start a connection to the database |
| Schema | `USAGE` | Resolve and access objects in the schema |
| Table | `SELECT` | Read table rows |
| Sequence | `USAGE` | Use sequence values during inserts |
Exercise
A role has SELECT on public.orders but receives “permission denied for schema public”. Which additional privilege should you inspect?
Show answer
Inspect USAGE on the public schema with hasschemaprivilege or \dn+. Table SELECT does not grant permission to use the schema that contains the table.