Sub topic 9
Inspecting roles and privileges
This chapter assumes you can connect with psql, use meta-commands such as \dt and \d+, and read the pg_catalog queries from the earlier chapters. It covers three questions: who can connect, what each role may do, and why a given query is allowed or refused.
Roles are users and groups
PostgreSQL has one concept for both users and groups: the role. A role with the LOGIN attribute can open a session. A role without it is normally used as a group that holds privileges. Roles belong to the whole server (cluster), not to one database, so \du shows the same list whichever database you are connected to.
Roles carry attributes that apply server-wide:
SUPERUSERbypasses all permission checks.CREATEDBandCREATEROLEallow creating databases and roles.REPLICATIONallows replication connections.BYPASSRLSskips row-level security policies.INHERIT(the default) means a member automatically uses the privileges of the roles it belongs to.
For the examples below, create a small setup. Run it as a superuser in a scratch database:
CREATE ROLE reporting NOLOGIN;
CREATE ROLE analyst LOGIN;
GRANT USAGE ON SCHEMA public TO reporting;
GRANT SELECT ON public.orders TO reporting;
GRANT reporting TO analyst;Here analyst can log in, and reporting is a group that holds the read privileges.
Listing roles with psql
The \du command lists roles with their attributes and group memberships. \du+ adds descriptions. \dg is an alias for the same command.
\du
\du+ analyst\du hides nothing about memberships in older versions. In recent versions, check \du+ and the catalog query below if the "Member of" column looks incomplete for your server version.
For scripting, query the catalog. Use the pg_roles view, not the pg_authid table. Any user can read pg_roles, while pg_authid is superuser-only because it holds password hashes. pg_roles always shows the password column as ********.
SELECT rolname, rolsuper, rolcreatedb, rolcreaterole, rolcanlogin
FROM pg_roles
WHERE rolname !~ '^pg_'
ORDER BY rolname;The filter removes the built-in pg_ roles such as pg_read_all_data. Those are real roles that you can grant, so remove the filter when you want to see them.
To see who belongs to which group, read pg_auth_members:
SELECT g.rolname AS group_role,
m.rolname AS member
FROM pg_auth_members am
JOIN pg_roles g ON g.oid = am.roleid
JOIN pg_roles m ON m.oid = am.member
ORDER BY g.rolname, m.rolname;To find the accounts that bypass every check, list superusers:
SELECT rolname FROM pg_roles WHERE rolsuper;Also run SELECT current_user, session_user; before you diagnose a permission error. current_user can differ from session_user after SET ROLE or inside a SECURITY DEFINER function.
Reading privileges with \dp
Each object stores an access control list (ACL). \dp (alias \z) prints it in the Access privileges column.
\dp public.ordersA result may look like this:
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+--------+-------+---------------------------+-------------------+----------
public | orders | table | postgres=arwdDxt/postgres+| |
| | | reporting=r/postgres | |Each entry has the form grantee=privileges/grantor. An empty grantee, as in =r/postgres, means PUBLIC, which is every role. The letters are:
| Letter | Privilege | Applies to |
|---|---|---|
| r | SELECT | tables, columns |
| a | INSERT | tables, columns |
| w | UPDATE | tables, columns |
| d | DELETE | tables |
| D | TRUNCATE | tables |
| x | REFERENCES | tables, columns |
| t | TRIGGER | tables |
| U | USAGE | schemas, sequences, types |
| C | CREATE | schemas, databases |
| c | CONNECT | databases |
| T | TEMPORARY | databases |
| X | EXECUTE | functions |
PostgreSQL 17 adds m (MAINTAIN) to tables, so the owner's string can be arwdDxtm.
The Access privileges column can also be completely empty. Empty does not mean "no access". It means the object still has its default ACL: the owner holds all privileges and nobody else holds any, apart from the PUBLIC defaults listed below.
Related commands:
\lshows database privileges (CONNECT,CREATE,TEMPORARY).\dn+shows schema privileges.\df+shows function ownership and privileges.\ddpshows default privileges set withALTER DEFAULT PRIVILEGES, which apply to objects created in the future.\d+ ordersshows the owner's row-level security policies at the bottom of its output.
Testing privileges directly
Reading ACLs does not account for membership, ownership, or superuser status. The has_*_privilege functions give the effective answer.
SELECT has_schema_privilege('analyst', 'public', 'USAGE') AS schema_usage,
has_table_privilege('analyst', 'public.orders', 'SELECT') AS can_select,
has_table_privilege('analyst', 'public.orders', 'DELETE') AS can_delete;For the setup above, this returns t, t, f. analyst has no direct grant on orders, but it inherits SELECT from reporting. The same family includes has_database_privilege, has_column_privilege, and has_function_privilege.
The standard views also expose grants. information_schema.table_privileges lists privileges for tables:
SELECT grantee, privilege_type
FROM information_schema.table_privileges
WHERE table_schema = 'public'
AND table_name = 'orders'
ORDER BY grantee, privilege_type;This view only shows grants where the current user, or a role it is a member of, is the grantor or the grantee. A non-superuser may therefore see fewer rows than \dp shows to an administrator, and a view that appears incomplete is not necessarily wrong.
How a role reaches an object
Access is checked in layers. A role needs USAGE on the schema and the specific privilege on the table, and either one can come through a group.
Gotchas
- PUBLIC defaults. Every role can
CONNECTto and createTEMPORARYtables in a new database. Every role canEXECUTEnew functions. Since PostgreSQL 15,PUBLICno longer hasCREATEon thepublicschema. On older servers it does. - Ownership. An owner has all privileges on its object and can grant them to others, and these rights do not appear as separate grants. Find owners with
\dtorSELECT tablename, tableowner FROM pg_tables. - Inheritance. If a role was created with
NOINHERIT, or a membership was granted withWITH INHERIT FALSEin PostgreSQL 16 and later, the member must runSET ROLE group_nameto use the group's privileges.has_table_privilegereports the effective result, which is why it is the best first check. - Column privileges. A role can have
SELECTon a few columns only. These appear in theColumn privilegescolumn of\dp, not in the main ACL. - Row-level security. Table privileges can be satisfied while a policy still hides rows. A query then returns zero rows with no error.
Roles in pgAdmin
In pgAdmin 4, expand the server and open Login/Group Roles. Select a role and open its Properties to see its attributes, or its Membership tab to see its groups. The Privileges tab shows privileges the role was granted at the server level.
For object privileges, open the object's properties (for a table, right-click and choose Properties) and look at the Security tab. Its Privileges list shows each grantee and the granted privileges. The SQL tab shows the GRANT statements pgAdmin generates for the object, which makes it easy to copy them into a script. This is the same information as \dp, shown in a form that is easier to scan for a single table.
Exercise
A role named intern was added to the group reporting, which holds SELECT on public.orders. \dp public.orders shows no entry for intern, yet intern can read the table. Name two checks that confirm why, and explain what each shows.
Show answer
First, run SELECT has_table_privilege('intern', 'public.orders', 'SELECT');. It returns t, which is the effective answer after membership and inheritance are counted. Second, query pg_auth_members joined to pg_roles, or run \du intern, to see that intern is a member of reporting. The access comes from the group's ACL entry reporting=r/..., not from a direct grant. Checking rolsuper for intern would rule out a superuser bypass.
Exercise
What does an empty Access privileges column in \dp mean, and what does an entry that begins with = mean?
Show answer
An empty column means the object has its default ACL: the owner has all privileges and no other role has any grants except the PUBLIC defaults. An entry that begins with =, such as =r/postgres, has an empty grantee, which means PUBLIC, so every role holds those privileges.