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.
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password. '; CREATE DATABASE master WITH OWNER sa; \c master; Now you are running this script using "psql" command line interface (CLI), so you get the message as below... CREATE ROLE CREATE DATABASE You are now connected to database "master" as user "postgres".
Use \du or \du+ psql command to list all users in the current database server. Use the SELECT statement to query the user information from the pg_catalog.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With