# Create user with all privileges in PSQL and add to db

afreddier

Alfredo Fernandez

Posted on April 14, 2022

# Create user with all privileges in PSQL and add to db

Check psql if is installed psql --version if you don't have installed then install from official page when installed make sure can see something like this

psql (PostgreSQL) 14.2 (Ubuntu 14.2-1.pgdg20.04+1+b1)
Enter fullscreen mode Exit fullscreen mode

check status of psql sudo service postgresql status look like this

ā— postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Thu 2022-04-14 17:20:08 -04; 3s ago
    Process: 38001 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 38001 (code=exited, status=0/SUCCESS)

abr 14 17:20:08 alfredo systemd[1]: Starting PostgreSQL RDBMS...
abr 14 17:20:08 alfredo systemd[1]: Finished PostgreSQL RDBMS.

Enter fullscreen mode Exit fullscreen mode

If service psql is inactive only need sudo service postgresql start

Run this command for create user

sudo -u postgres createuser testuser
# For crete db
sudo -u postgres createdb testdb
Enter fullscreen mode Exit fullscreen mode

Now we make setting for nice work with that user and the database
First enter to psql

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

For see all user list you can run \du
when see all user list we cannot see any roles even doesn't exist password for this user , we need add some privileges inside db (postgres)

ALTER USER testuser with encrypted password 'secret';
# If this command run success, message look like this
# ALTER ROLE
grant all privileges on database testdb to testuser;
# GRANT
Enter fullscreen mode Exit fullscreen mode

when list again with \l the database has attach to new user

šŸ’– šŸ’Ŗ šŸ™… šŸš©
afreddier
Alfredo Fernandez

Posted on April 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related