CURRENT_USER returns the name of the role that is executing the current SQL statement.
CURRENT_USER yields the name of the role that is actively running the statement. It reflects privilege inheritance after SET ROLE or SECURITY DEFINER functions, making it ideal for auditing.
Run SELECT CURRENT_USER;
. PostgreSQL returns a single-row, single-column result containing the current role, e.g., galaxy_dev
.
Yes. Combine it with tables like Orders
to stamp who issued a report or update:
SELECT CURRENT_USER AS run_by,
NOW() AS run_at,
COUNT(*) AS total_orders
FROM Orders
WHERE order_date > CURRENT_DATE - INTERVAL '30 days';
Add a trigger that inserts CURRENT_USER
into updated_by
on every change. This keeps a clear editing trail.
Yes. Inside a SECURITY DEFINER
function, CURRENT_USER
returns the function owner, not the caller. Use SESSION_USER
when you need the original login role instead.
Use it for row-level security predicates, audit columns, and session context checks. Avoid hard-coding role names; rely on the dynamic value of CURRENT_USER
.
Don’t confuse USER
and CURRENT_USER
; they are synonyms in PostgreSQL but differ in other SQL dialects. Always test inside SECURITY DEFINER code to verify you get the expected role.
Yes, in PostgreSQL USER
and CURRENT_USER
are synonyms. Both follow role changes.
Absolutely. Row-level security (RLS) policies commonly compare CURRENT_USER
to owner columns to restrict access.
Yes. The function is evaluated at runtime, so any view that references it adapts to the role executing the SELECT.