Menu

How to Install PostgreSQL on Ubuntu 20.04: A Step-by-Step Tutorial

Install PostgreSQL from Ubuntu’s repository on an existing Ubuntu 20.04 system. Standard security maintenance ended in May 2025; Ubuntu Pro coverage may apply through May 2030.

Posted on By Updated on
On this page

This guide covers installing Ubuntu’s PostgreSQL package on an existing Ubuntu 20.04 LTS system. Ubuntu 20.04 is past standard security support, so check your system’s coverage before using it for a new deployment.

Support status: Ubuntu 20.04 LTS standard security maintenance ended in May 2025. Ubuntu lists Ubuntu Pro coverage through May 2030; ESM requires the applicable Ubuntu Pro subscription and enabled coverage. See Ubuntu’s security maintenance schedule and confirm that your system and packages are covered. For current LTS releases, see our guides for Ubuntu 24.04 and Ubuntu 26.04.

Preconditions

Before you begin the installation process, ensure you have met the following prerequisites:

  1. An Ubuntu 20.04 LTS system.
  2. Access to a user account with sudo privileges.
  3. A stable internet connection to download PostgreSQL packages.
  4. Familiarity with basic command-line operations in Ubuntu.

With these prerequisites in place, let’s proceed with the installation.

Installing PostgreSQL on Ubuntu 20.04

Step 1: Update the System

Start by ensuring your Ubuntu 20.04 LTS system is up to date. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

This command will update the package list and upgrade existing packages on your system.

Step 2: Install PostgreSQL

To install PostgreSQL on Ubuntu 20.04, use the following command:

sudo apt install postgresql postgresql-contrib

This command will install both the PostgreSQL server and additional contrib packages that provide useful extensions and utilities.

Step 3: Start and Enable PostgreSQL

To start the PostgreSQL service and enable it to start automatically at boot, use these commands:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Creating a PostgreSQL User and Database

Access the PostgreSQL Prompt

Let’s access the PostgreSQL command-line tool, psql, to create a new PostgreSQL user and database. Use the following command:

sudo -u postgres psql

Create a PostgreSQL User

Within the psql prompt, you can create a new PostgreSQL user by running the following SQL command. Replace your_user and your_password with your preferred values:

CREATE USER your_user WITH PASSWORD 'your_password';

Create a PostgreSQL Database

Next, create a new PostgreSQL database and assign it to the user you created in the previous step:

CREATE DATABASE your_database;
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_user;

Creating a Table

To illustrate PostgreSQL’s capabilities, let’s create a simple table in the newly created database. Still within the psql prompt, execute the following SQL commands to create a basic table:

\c your_database
CREATE TABLE example (
    id serial PRIMARY KEY,
    name VARCHAR (100),
    age INT
);

Managing the PostgreSQL Service

To manage the PostgreSQL service on Ubuntu 20.04, you can use the following commands:

  • Start PostgreSQL service: sudo systemctl start postgresql

  • Stop PostgreSQL service: sudo systemctl stop postgresql

  • Restart PostgreSQL service: sudo systemctl restart postgresql

  • Check PostgreSQL service status: sudo systemctl status postgresql

Conclusion

Congratulations! You have successfully installed PostgreSQL on your Ubuntu 20.04 LTS system, created a database, and learned how to perform basic management tasks. PostgreSQL’s extensive feature set makes it a versatile choice for various data storage needs.

With PostgreSQL now installed and running, you are well-prepared to build and manage databases for your Ubuntu-based applications, leveraging its scalability and reliability for your data-driven projects. Explore PostgreSQL’s documentation to discover its full potential and adapt it to your specific requirements.

If you want to learn more about PostgreSQL, please use our PostgreSQL tutorials and PostgreSQL Reference.