Install PostgreSQL on Ubuntu: An Easy-to-Follow Guide

Here, we are going to look at how to install PostgreSQL on Ubuntu.

Install PostgreSQL on Ubuntu

In this article we’re going to cover how to install PostgreSQL on Ubuntu from the official Ubuntu repository or by adding the PostgreSQL repository and installing the same.

PostgreSQL, also known as “Postgres”, is an advanced, enterprise class open source relational database management system (ORDBMS). It is developed by a worldwide team of volunteers and it is quite popular due to its stability and advanced features. PostgreSQL is not controlled by any corporation or other private entity and the source code is available free of charge.

Most Linux distributions such as Debian, CentOS, openSUSE, and Ubuntu have PostgreSQL integrated with their package management. In this tutorial we are going to show you how to install and use the version available by default with your version of Ubuntu as well as how to install it by adding the PostgreSQL repository and installing the same.

Install PostgreSQL on Ubuntu

Install PostgreSQL from the Official Ubuntu Repository

We recommend that you install PostgreSQL this way since it ensures a proper integration with the operating system including automatic patching and other update management functionality.

First, as always, update your packages:

sudo apt update

Then, install the postgresql package along with a postgresql-contrib package. The addition of the -contrib package ensures that you get some extra utilities and features.

So, to install the default supported PostgreSQL version provided by Ubuntu’s repository, run:

sudo apt install postgresql postgresql-contrib

That’s all. By default, the software creates a postgres user, once you successfully install the database system. This user account has the default ‘postgres’ role.

Install PostgreSQL by Adding the PostgreSQL Repository

To start off, you first need to add PostgreSQL repository to Ubuntu system’s sources list.

1. Install few required packages:

sudo apt install wget curl ca-certificates

2. You need to download and import the PostgreSQL GPG repository key:

wget -O - | sudo apt-key add -

3. Now, execute the following command to create the file repository configuration:

sudo sh -c 'echo "deb $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

4. Finally, update the repository list and install PostgreSQL on your Ubuntu system:

sudo apt update
sudo apt install postgresql

To be able to install a specific version from the PostgreSQL repository, instead of just postgresql which refers to the latest version, specify it like postgresql-12.

At the time of writing, the available versions are: postgresql-10, postgresql-11, postgresql-12, and postgresql-13.

sudo apt install postgresql-12

Once installation completes, you can check the status of the PostgreSQL service:

sudo systemctl status postgresql.service
postgresql.service - PostgreSQL RDBMS
      Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
      Active: active (exited) since Sun 2021-07-11...
Jul 11 18:37:48 ubuntu systemd[1]: Starting PostgreSQL RDBMS...
Jul 11 18:37:48 ubuntu systemd[1]: Finished PostgreSQL RDBMS.

Congratulations! You have successfully managed to install PostgreSQL on Ubuntu.

Connect to PostgreSQL

postgres user gets created automatically post-installation, which has superadmin access to the DB instance. To establish a connection with the newly set-up database, switch to this account as:

sudo su - postgres

Then you can access the PostgreSQL prompt by typing:

psql (13.3 (Ubuntu 13.3-1.pgdg20.04+1))
Type "help" for help.


To quit the PostgreSQL prompt, just type \q.

Of course, instead of switch users to connect to PostgreSQL, you can do so with a single command:

sudo -u postgres psql


PostgreSQL is widely adopted databases in the enterprise world. Whether you decide to install it from the official Ubuntu repository or the PostgreSQL repository, both installations are simple and easy to do.

If you’d like to learn more about PostgreSQL and how to use it, there is a tutorial available on the project’s website.

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge