How to install PostgreSQL on Ubuntu 22.04

This tutorial will take you step-by-step through the installation of PostgreSQL on Ubuntu 22.04, explaining all necessary setup steps along the way. It will also show you how to create and manage your first PostgreSQL database.

PostgreSQL, also known as Postgres, is an open-source relational database management system (RDBMS) that emphasizes extensibility and SQL compliance. It’s widely used for its robustness, advanced features, and powerful performance. This article provides a comprehensive guide on how to install PostgreSQL on Ubuntu 22.04.

Before we dive into the installation process, it’s important to understand what PostgreSQL is and why it’s a popular choice for many developers and organizations. PostgreSQL supports both SQL (relational) and JSON (non-relational) querying, making it a versatile choice for a variety of applications. It also offers a range of features such as complex queries, foreign keys, triggers, updatable views, transactional integrity, and multiversion concurrency control.


Before we can install PostgreSQL on Ubuntu 22.04, there are a few prerequisites to check:

  1. You should have Ubuntu 22.04 installed on your server.
  2. You should have superuser (sudo) privileges.
  3. You should have access to a terminal/command line.
  4. You should have an internet connection to download the necessary packages.

Step 1: Update System Packages Index

Before we begin, we need to update the system’s default applications and packages. Open the terminal and type the following command:

sudo apt update && sudo apt upgrade -y

This command will update and upgrade all the packages and applications installed on your Ubuntu system.

Step 2: Install PostgreSQL on Ubuntu

Now that the system packages have been updated, we can install PostgreSQL. Enter the following command in the terminal:

sudo apt install postgresql postgresql-contrib -y
How to install PostgreSQL on Ubuntu 22.04

Using this command, PostgreSQL and some additional tools will be installed.

PostgreSQL will automatically create a new user account called “postgres” and a new database also called “postgres”.

Step 3: Verify Installation

We can check PostgreSQL’s service status to make sure the installation went smoothly. Enter the command below into the terminal:

sudo systemctl status postgresql
How to install PostgreSQL on Ubuntu 22.04

The output should indicate that the PostgreSQL service is active (running) if PostgreSQL is functioning properly.

Step 4: Accessing PostgreSQL on Ubuntu

PostgreSQL employs a role-based authentication system by default. As a superuser, the ‘postgres’ user has full administrative authority. Type the following command to sign in as the ‘postgres’ user:

sudo su - postgres

Then, to access the PostgreSQL prompt, type:


You’ll now be at the PostgreSQL prompt, where you can issue SQL commands.

Step 5: Creating a New Role

With the ‘createuser’ command in PostgreSQL, you can create new roles directly from the command line. ‘Myuser’ will be the name of our new role. First, hit enter after typing “q” to end the PostgreSQL prompt. then enter the command as follows:

createuser --interactive --pwprompt

You will be prompted by the system to enter a password and the name of the new role (we’ll use “myuser”). You will then be prompted to choose whether the new role should be a superuser. It’s safer to reply “no” for security reasons unless you specifically require superuser privileges for this role.

Step 6: Creating a New Database

With the ‘createdb’ command, you can create new databases just like you can create new roles from the command line. Let’s start by making a new database called “mydb.” Use the following command:

createdb mydb

Step 7: Accessing the New Database

To access the new database with the new role, first exit the ‘postgres’ user shell by typing ‘exit’ and hitting enter. Then, type the following command:

psql -d mydb -U myuser

You’ll be prompted to enter the password for ‘myuser’. After entering the password, you’ll be at the PostgreSQL prompt for the ‘mydb’ database.

Final Thoughts

Congratulations! You’ve created your first PostgreSQL on Ubuntu database and user as well as successfully installed PostgreSQL on Ubuntu 22.04. You can now build robust, sophisticated databases for your applications using PostgreSQL. Consider exploring PostgreSQL’s numerous features to see how you can tailor it to meet your unique needs. Keep in mind that PostgreSQL is highly customizable.


How To Install CouchDB On Ubuntu 22.04 - Virtono Community · September 14, 2023 at 4:26 PM

[…] is a document-oriented NoSQL database that uses JSON to store data, JavaScript as its query language, and HTTP for an API. It offers […]

How to install PostgreSQL on Ubuntu 22.04 – thodorisgudis · December 18, 2023 at 9:51 AM

[…] Prerequisites […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.