postgresqlpsql

How do you change a user in PostgreSQL?


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?


Solution

  • When you display the psql online help by entering \? you can see:

    Connection
      \c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
                             connect to new database (currently "postgres")
    

    So you need to use:

    \c shorturl shorturl