Learning center

Identify the effective PostgreSQL role behind a denied statement

Read current_user when diagnosing a statement's ordinary privilege checks. session_user usually identifies the role that opened the connection. They can differ after a role switch or inside a security-definer function.

In a fictional reporting service, the pool connects as report_pool and changes role for the request. A query on reporting.daily_sales fails. Use the same role setup in an authorized reproduction and query the session identity just before the equivalent read.

SELECT current_database(), session_user, current_user;

If the result is reports, report_pool, report_guest, investigate report_guest on the target table. For SQL executed inside a security-definer function, inspect the function's owner and security mode. A query run outside the function reports the outer role. When the failure is inside an aborted transaction, read the role in a fresh controlled reproduction or from diagnostics captured before the error. PostgreSQL session information functions ยท CREATE FUNCTION security modes

Trace another denied request.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading