Knowledge Base
MilesWeb / VPS FAQ

Discover the Steps to Install PostgreSQL on Ubuntu 18.04

Approx. read time : 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.

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent 4.8 out of 5 based on reviews. Read more reviews.

Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna