Returns the name of the user running the current query.
Syntax
Return Types
TEXT
Examples
SELECT SESSION_USER() as user;
Rows: 1Execution time: 5.75ms
The following code example shows the effective privileges of the roles directly assigned to the user running the query:
SELECT
AR.grantee,
AR.role_name,
OP.privilege_type,
OP.object_type,
OP.object_name
FROM information_schema.applicable_roles AS AR
JOIN information_schema.object_privileges AS OP
ON (AR.role_name = OP.grantee)
WHERE
AR.grantee = session_user();
| grantee text | role_name text | privilege_type text | object_type text | object_name text |
|---|
| demoproxy | proxy_role | SELECT | table | usaccidentdata |
| demoproxy | proxy_role | SELECT | table | tournaments |
| demoproxy | proxy_role | SELECT | table | t |
| demoproxy | proxy_role | SELECT | table | rankings |
| demoproxy | proxy_role | SELECT | table | playstats |
| demoproxy | proxy_role | SELECT | table | players |
| demoproxy | proxy_role | SELECT | table | levels |
| demoproxy | proxy_role | SELECT | table | games |
| demoproxy | proxy_role | USAGE | schema | public |
Rows: 9Execution time: 40.41ms
Example
Dynamic security through a view which uses session_user().
-- user bob created view:
create view my_employee_data as select * from employees where user_name = session_user();
-- user alice queries it:
select * from my_employee_data; -- session_user() will be evaluated to 'alice' for this query
Returns