PostgreSQL Tutorial: Install PostgreSQL on Ubuntu using apt

October 12, 2023

Summary: in this tutorial, you will learn how to download and install PostgreSQL on Ubuntu using apt.

Most Linux platforms such as Debian, Red Hat / CentOS, SUSE, and Ubuntu have PostgreSQL integrated with their package management.

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

To download and install PostgreSQL on Linux, you visit the download page and select your Linux distribution.

In this tutorial, we’ll show you how to install PostgreSQL on Ubuntu 20.04.

Install PostgreSQL on Ubuntu

Once visiting the PostgreSQL download for Ubuntu page, you find the script that allows you to install PostgreSQL on Ubuntu. You need to execute them one by one

First, 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'

Second, import the repository signing key:

$ wget --quiet -O - | sudo apt-key add -

Third, update the package list:

$ sudo apt-get update

Finally, install the latest version of PostgreSQL:

$ sudo apt-get install postgresql

If you want to install a specific version, you can use postgresql-version instead of postgresql. For example, to install PostgreSQL version 12, you use the following command:

$ sudo apt-get install postgresql-12

It will take few minutes to download and install the PostgreSQL.

Connect to the PostgreSQL database server via psql

In PostgreSQL, a user account is referred to as a role. By default, PostgreSQL uses ident authentication.

It means that PostgreSQL will associate its roles with the system accounts of Linux. If a role exists in PostgreSQL, the same Linux user account with the same name is able to log in as that role.

When you installed PostgreSQL, the installation process created a user account called postgres associated with the default postgres role.

To connect to PostgreSQL using the postgres role, you switch over to the postgres account on your server by typing:

$ sudo -i -u postgres

It’ll prompt for the password of the current user. You need to provide the password and hit the Enter keyboard.

Then, you can access the PostgreSQL using the psql by typing the following command:

$ psql

You’ll access the postgres prompt like this:


From here, you can interact with the PostgreSQL like issuing a query.

To quit the PostgreSQL prompt, you run the following command:

postgres=# \q

This above command will bring you back to the postgres Linux command prompt.


To return to your regular system user, you execute the exit command like this:

postgres@ubuntu-dev:~$ exit

Load the sample database

To load the sample database into the PostgreSQL database server, you follow these steps:

First, switch over the postgres account using the following command:

$ sudo -i -u postgres

It’ll prompt you for the password of the current user. You need to type the password of the current user and press the Enter keyboard.

Second, download the sample database using the curl tool:

$ curl -O

Third, unzip the file to get the dvdrental.tar file:

$ unzip

Fourth, access the PostgreSQL using the psql tool:

$ psql

Fifth, create the dvdrental database using the CREATE DATABASE statement:

postgres=# create database dvdrental;

Sixth, quit the psql by using the \q command:

postgres=# \q

Seventh, use the pg_restore tool to restore the dvdrental database:

$ pg_restore --dbname=dvdrental --verbose dvdrental.tar

Eighth, access PostgreSQL database server again using psql:

$ psql

Ninth, switch to the dvdental database:

postgres=# \c dvdrental

Now, you’re connected to the dvdrental database:


Finally, enter the following command to get the number of films in the film table:

dvdrental=# select count(*) from film;

Here is the output:

(1 row)

Congratulations! you have successfully installed PostgreSQL on Ubuntu, connect to PostgreSQL database server using psql, and load the sample database.