EXECUTE AS

Use the EXECUTE AS command to set the CURRENT USER of the session.

Synopsis

Syntax for setting the current user:
EXECUTE AS <target-user-name>

Inputs

The EXECUTE AS command has the following input:
Table 1. EXECUTE AS input
Input Description
<target-user-name> The name of an existing user.

Outputs

The EXECUTE AS command has the following outputs:
Table 2. EXECUTE AS outputs
Output Description
EXECUTE AS The message that the system returns if the command is successful.
ERROR: permission denied. You must have EXECUTE AS privilege on the target user.

Description

This statement allows a suitably privileged user to set the CURRENT USER of their session to a different user. On successful completion of this statement, the CURRENT USER is the target user specified in the syntax. Security checks are based on the security profile the CURRENT USER.

The current_user function returns the target user. The session_user function returns the original session user.

EXECUTE AS operates outside of the transaction scope, just like SET statements. Running EXECUTE AS while in an explicit transaction is allowed. Rolling back the transaction does not affect the EXECUTE AS setting. For example, suppose that you are running as jdoe and running the following:
BEGIN;
EXECUTE AS dd;
ROLLBACK;
SELECT current_user;
CURRENT_USER 
--------------
DD
(1 row)
Privileges required
You must have EXECUTE AS privilege on the target user.
Common tasks
Use the EXECUTE AS command to set the CURRENT USER of the session.

Usage

The following provides sample usage:
EXECUTE AS BOB;