20

I have some application based on Qt library and using QPSQL driver.
In PostreSQL defined a few user roles (e.g: admin, operator, user). My application creates a connection to postgres server under specified user. How can I check the users role?

Maksim Nesterenko
  • 4,863
  • 8
  • 49
  • 82

2 Answers2

48

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
  • 539,169
  • 125
  • 977
  • 1,137
  • 3
    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
7

You can check PostgreSQL user permissions with this query:

SELECT * FROM pg_roles;
hank
  • 8,883
  • 2
  • 31
  • 47