How do you change a user in PostgreSQL?

Viewed 48642
postgres=# \du
                         List of roles
Role name |                   Attributes                   | Member of 

-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 shorturl  | Superuser                                      | {}

postgres=# create database shorturl;
CREATE DATABASE

postgres=# \c shorturl;
You are now connected to database "shorturl" as user "postgres".

shorturl=# 

My preference was to use database shorturl being the user shorturl and not postgres. How do I change the same?

Thanks in advance.

2 Answers

Another option, if you're already connected to database shorturl, is the command:

SET ROLE shorturl;

The benefits of that command are that it's not tied to psql, and that you can change user "midstream" of your work.

Related