How To Install PostgreSQL on Ubuntu 21.10

PostgreSQL is a powerful, open-source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.

Install PostgreSQL on Ubuntu

Install PostgreSQL on Ubuntu by using the following command.

sudo apt update

sudo apt install postgresql postgresql-contrib


root@crowncloud:~# apt install postgresql postgresql-contrib
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
postgresql-contrib is already the newest version (13+225ubuntu1).
The following packages were automatically installed and are no longer required:
  libfile-find-rule-perl libnumber-compare-perl libtext-glob-perl

PostgreSQL Roles and Databases

We'll switch to a postgres account for this.

sudo -i -u postgres

you can access a PostgreSQL prompt using the psql utility.



postgres@crowncloud:~$ psql
psql (13.4 (Ubuntu 13.4-1))
Type "help" for help.

To exit out of the PostgreSQL shell type.


To Create PostgreSQL Role

sudo -u postgres createuser --interactive


Enter name of role to add: john
Shall the new role be a superuser? (y/n) y

To create a PostgreSQL database

sudo -u postgres createdb john

To open a Postgres Prompt with the New Role.

sudo adduser john

To switch over and connect to the database.

sudo -u john psql


root@crowncloud:~# sudo -u john psql
could not change directory to "/root": Permission denied
psql (13.2 (Ubuntu 13.2-1))
Type "help" for help.


Once you logged in as john and check your current connection information.



john=# \conninfo
You are connected to database "john" as user "john" via socket in "/var/run/postgresql" at port "5432".