PostgreSQL: create a user with DB

PostgreSQL: create a user, a database and grant accesses

The following step-by-step will let you create a user, a database (DB) and grant full access to the user to this DB.

All the commands are executed as the postgres privileged user.

Create the user

For this, you use the command createuser which is provides with the postgreSQL package. Then answer the questions as you see fit :

postgres@hostname:~$ createuser
Enter name of role to add: username
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

Create the DB

Use the createdb command to create the database :

postgres@hostname:~$ createdb databasename

Grant access for the user to the DB

And last, using the psql command, set a password for the user and grant accesses:

postgres@hostname:~$ psql
postgres=# alter user username with encrypted password 'password';
postgres=# grant all privileges on database databasename to username;

Last updated on 2022:12:20 at 22:52 UTC

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.