May 15, 2020
Postgresql — Create a user, create a database
We expect that you have a running instance of Postgresql on your pc.
Login to the Postgresql through admin account.
$psql -U postgres
Create a new role.
postgres=# create role myuser with login password ‘abc123’;
Add CREATEDB permission to our new user to allow them to
create databases
postgres=# alter role myuser createdb;
Quit the pg shell
postgres=#\q
Login to using newly created user
$psql -U myuser -d postgres
Create a database
postgres=>create database mydatabase;
Grant all privileges on the database to a user
postgres=>grant all privileges on database mydatabase to myuser;
Connect to the database
postgres=>\connect mydatabase
Show connection details
mydatabase=>\conninfo
Show roles list
mydatabase=>\du
Show database list
mydatabase=>\l