SET SESSION AUTHORIZATION

Sets the current and session user for the current database connection. You can change session authorization to execute queries as another user for testing or debugging purposes, or to limit query access.

Syntax

SET SESSION AUTHORIZATION { username | DEFAULT }

Parameters

username
The name of the user that you want to authorize for the current SQL session.
DEFAULT
Sets session authorization to the dbadmin user.

Privileges

Superuser

Examples

In the following example, the dbadmin gives the debuguser user session authorization, and then changes the session authorization back to the dbadmin user.

  1. Verify the current user and session user:

    => SELECT CURRENT_USER(), SESSION_USER();
     current_user | session_user
    --------------+--------------
     dbadmin      | dbadmin
    (1 row)
    
  2. Set authorization for the current session to debuguser, and verify the changes:

    => SET SESSION AUTHORIZATION debuguser;
    SET
    => SELECT CURRENT_USER(), SESSION_USER();
     current_user | session_user
    --------------+--------------
     debuguser    | debuguser
    (1 row)
    
  3. After you complete debugging tasks, set the session authorization to DEFAULT to set the current and session user back to dbadmin user, and verify the changes:

    => SET SESSION AUTHORIZATION DEFAULT;
    SET
    => SELECT CURRENT_USER(), SESSION_USER();
     current_user | session_user
    --------------+--------------
     dbadmin      | dbadmin
    (1 row)