How do I create a user and grant privilege in PostgreSQL from the Linux terminal?

Posted 4 weeks ago
How do I create a user and grant privilege in PostgreSQL from the Linux terminal?

PostgreSQL is a free and open-source relational database management system. It perfectly aligns itself with SQL standards.It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. 


In this post we will see how to create a user and PostgreSQL database management system and how to grant him required privileges.


For this article, I’m using the Ubuntu 20.04 system. Before starting with this tutorial, kindly confirm that you have properly installed PostgreSQL on your Ubuntu Linux system.


1. Launch your linux terminal.


Launch your linux terminal On Bitcolon


2. On Terminal start PostgreSQL using the following command:


> `sudo -i -u postgres`


On Terminal start PostgreSQL using the following command


3. On the next stage enter `psql` command on terminal prompt.


> psqlOn the next stage enter `psql` command on terminal prompt.


4. Now, you have entered the psql shell. Create a user using this command.


postgres=# create user bitcolon with encrypted password 'password';


psql shell. Create a user using this command


5. Create a database using this command.


postgres=# create database bitcolon_db;


Create a database using this command.


6. Grant all privileges on the database to specific users using this command.


postgres=# grant all privileges on database bitcolon_db to bitcolon;


Grant all privileges on the database to specific users using this command.