How to switch role after connecting to database in PostgreSQL

In this article, we will see how to switch role after connecting to database in PostgreSQL. We can switch current connected user/role using set role command.

Switch role after connecting to database in PostgreSQL:

1. Lets connect to PostgreSQL

psql -h localhost -p 5432 -U postgres -p

Now verify the current user and session user by running below query.

select current_user, session_user;

2. Now switch the role from postgres to james.

set role james;

Now verify the current user and session user by running below query.

select current_user, session_user;

How to switch role after connecting to database in PostgreSQL