Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  VPS FAQ  >  Discover the Steps to Install PostgreSQL on Ubuntu 18.04
Top Scroll

Discover the Steps to Install PostgreSQL on Ubuntu 18.04

 3 min

Relational Databases are an important part of different applications and websites. These databases help to continue data and offer a logical structuring to it. The data can be queried and accessed at any time and this is one of the biggest advantages of it.

If you want to find an alternative for MySQL, you will find multiple relational databases and one of them is the PostgreSQL. It is very popular and used by several applications.

Being an open source, PostgreSQL is compatible with most OS, including Unix based systems.

This article offers the steps to install PostgreSQL on Ubuntu 18.04. You will be using Ubuntu’s default repository with PostgreSQL packages.

Installation of PostgreSQL on Ubuntu is simple. But, in the post-installation, you will need to create an appropriate database and set user roles for everything to work well.

Steps to Install PostgreSQL on Ubuntu

Prior to starting these steps you will need to access your VPS through SSH.

At first you will use apt to update the local package index:

sudo apt update

Then, install PostgreSQL and the contrib package that offers additional features using the command below:

sudo apt install postgresql postgresql-contrib

With this command the PostgreSQL will get installed. For using it, do some basic configuration.

Steps to Verify PostgreSQL on Ubuntu

After completing the installation of Postgres on Ubuntu, the services will start automatically. Now, verify the installation by connecting to the PostgreSQL database with psql.

The psql is a command line utility that enables you to interact with the PostgreSQL server. This will print the server version:

sudo -u postgres psql -c "SELECT version();"

You can use the psql to log in to PostgreSQL as a postgre user. Use the below command for this:

sudo su – postgres

Type the below command to use PostgreSQL:


With this you will get access to PostgreSQL instance. For exiting, you can use:


Role and User Creation

As done above for creating a role, log in to the postgres account. After being at the Postgres console, you can create a new role with the below command:

createrole --interactive

You will be asked to name the role and if it has superuser permission. Similarly, it is possible to create a new user with the below command:

createuser –interactive

Otherwise, from the normal command prompt you can use the below command:

sudo -u postgres createuser –interactive

Again you will be asked for a user name. You can use the createuser command with multiple options which can be checked by using the createuser.

In this way, you have created a new user. Then, you will learn to create a Database.

Database Creation

With PostgreSQL, it is assumed that the role name should be the same as the database name. That indicates if you created a user named “testPostgres” earlier, then the role will try to access a database named “testPostgres”.

You can create a database from the Postgres console with the below command:

createdb testPostgres

If you are working without the Postgres SQL, you can use a command as displayed below:

sudo -u postgres created testPostgres

Steps to Validate the New Role

First, you need to create a Linux user with the same names as the Postgres DB and the role. You can create it using a non-root account with sudo access. For creating the new user in Linux, use a command similar to the one shown below:

sudo adduser testPostgres

Then replace the name testPostgres with a proper name.

After the account is created, switch to it and connect to the database using the below command:

sudo -u testPostgres psql

For switching to another database, mention the database name with the below command:

psql -d sampleUser

After logging in, check the current DB connection with the below command:


Postgres Basic DML Operation

The similar syntax is used by Postgres to other databases for functionalities such as creation, deletion and updating tables. A new table can be created and you can describe its definition using the below command:

\d <TableName>

To view the table without a sequence, use the below command:

\dt <TableName>

Wrapping Up

So, you might have not got a basic understanding of Postgres. You have learned to install Postgres on Ubuntu 18.04 and understood the basic setup of it to create a user, roles, and database.

This tutorial will help you to get started with Postgres on Ubuntu.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
Claim the discount before it’s too late. Use the coupon code:
Note: Copy the coupon code and apply it on checkout.