Switch role after connecting to database in PostgreSQL

In this article, we will see how to Switch role after connecting to database in PostgreSQL with examples.

We can switch or change the current use/role as active using set command.

We need to make the current session user a member of the role:

Syntax:

set role myrole;

Switch role after connecting to database in PostgreSQL

Lets verify whether current active role after switching:

SELECT current_user,session_user;

Where
session_user is user connected to PostgreSQL
current_user is the user we just switched to