29

I have an application (based on the Qt library) which uses QPSQL driver.
In PostgreSQL, there are a few user roles defined (e.g: admin, operator, user). My application creates a connection to the Postgres server under a specified user. How can I check the user's role?

Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91

2 Answers2

65

The manual:

SELECT current_user;  -- user name of current execution context
SELECT session_user;  -- session user name

Meaning, session_user shows the role you connected with, and current_user shows the role you are currently operating with, for instance after calling SET role some_other_role;.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • 5
    Just for the sake of completeness, if you want to know a set role from within a function with `SECURITY DEFINER`, use `current_setting('role')` as [can be seen on the mailing list](https://www.postgresql.org/message-id/13906.1141711109%40sss.pgh.pa.us) – mlt Jan 31 '20 at 00:20
10

You can check PostgreSQL user permissions with this query:

SELECT * FROM pg_roles;
hank
  • 9,553
  • 3
  • 35
  • 50