How to Install PostgreSQL on Ubuntu 18.04

January 9, 2020


PostgreSQL is an open-source, object-relational database system with a strong reputation for feature robustness, extensibility, and technical standards compliance.

The latest version of this database system is PostgreSQL 12.1, while versions 11.6, 10.11, 9.6.16, 9.5.20, and 9.4.25 still get regular support updates.

In this tutorial, you will learn about two different ways of installing the newest PostgreSQL on Ubuntu 18.04.

How to Install PostgreSQL on Ubuntu.


  • An Ubuntu 18.04 Linux system
  • Access to a command-line/terminal window (Ctrl+Alt+T)
  • A user account with sudo privileges

Install PostgreSQL from PostgreSQL Apt Repository

PostgreSQL is available in all Ubuntu versions by default, but it doesn’t guarantee automatic updates when new releases come out. The local repository only has "snapshots" of a specific version. The best practice is to install the software from the PostgreSQL Apt Repository.

The PostgreSQL Apt Repository provides the latest PostgreSQL version, as well as all previous server packages, extensions, and modules.

Step 1: Add PostgreSQL Repository

To install from the official repository, you first need to add it to your system.

Import the GPG repository key with the commands:

sudo apt-get install wget ca-certificates
wget --quiet -O - | sudo apt-key add -
import postgresql apt repository

Then, add the PostgreSQL repository by typing:

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

Step 2: Update the Package List

After adding the official PostgreSQL repository, make sure to update the package list. Doing this ensures you install the latest PostgreSQL package.

sudo apt-get update

Step 3: Install PostgreSQL

To install PostgreSQL and the PostgreSQL contrib package (which provides additional features), use the following command:

sudo apt-get install postgresql postgresql-contrib
command to install postgresql on ubuntu

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 from Local Ubuntu Repository

If you prefer installing PostgreSQL from the local Ubuntu repository, bear in mind that it is most likely not going to be the latest version of the package.

Step 1: Check Available PostgreSQL Version

Before you decide whether you want to set up PostgreSQL from the Ubuntu repository, verify which versions are available. Update the repository and then run the command:

apt show postgresql

The output provides all the necessary information about the package, including the release number and size.

Command for checking PostgreSQL version on Ubuntu.

Step 2: Install PostgreSQL Package

If you are happy with the PostgreSQL version accessible from the local repository, use the following command to install the package:

sudo apt install postgresql postgresql-contrib

With these simple steps, you have successfully installed PostgreSQL on Ubuntu 18.04.

Connect to PostgreSQL

To establish a connection with the newly set-up database, log into the postgres account with:

sudo su - postgres

Now open a postgress prompt using the command:

Accessing Postgres through terminal.

Check Connection Information

If you are connected to PostgreSQL and want to see details of the connection, use the command:


The output displays information on the database name, the account you are logged in, the socket path, and port number.

Checking information on postgres connection.


This article should help you set up PostgreSQL. Whether you decide to install from the PostgreSQL repository or the local Ubuntu repository, both installations are simple and easy to do.

To connect to PostgreSQL and manage SQL queries check out our article on setting up and connecting PostgreSQL with SQL Workbench.

To learn more about PostgreSQL installation, make sure to read our article How to Download and Install PostgreSQL on Windows.

Was this article helpful?
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
Install and Get Started with MySQL Workbench on Ubuntu 18.04
January 9, 2020

Workbench is a visual tool for managing MySQL databases. Its graphical interface allows administrators and...
Read more
How to List All Users in a MySQL Database
November 18, 2019

This simple tutorial analyses the commands used to list all user accounts in MySQL. Learn about additional...
Read more
How to Install MariaDB 10.4 on Ubuntu 18.04
February 28, 2024

MariaDB is an open-source, fully compatible, relational database management system. It is commonly used as a...
Read more
How to Connect to a PostgreSQL Database From Command Line in Linux
February 22, 2024

PostgreSQL is an open-source relational database management system. In this tutorial learn how to connect...
Read more