How to Install PostgreSQL on Ubuntu 22.04

Install PostgreSQL on Ubuntu 22.04

In this article, we will have explained the necessary steps to install PostgreSQL on Ubuntu 22.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

PostgreSQL is a powerful, open-source object-relational database system. It differs from relational database management systems such as MySQL in that even complex data objects can be stored relationally in the database.

Prerequisite:

  • Operating System with Ubuntu 22.04
  • Server IPv4 Address with Superuser Privileges (Root Access)
  • Gnome Terminal for Linux Desktop
  • PuTTy SSH client for Windows or macOS
  • Powershell for Windows 10/11
  • Familiar with APT Commands

Install PostgreSQL on Ubuntu 22.04 LTS

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade
sudo apt install apt-transport-https curl wget

Step 2. Install PostgreSQL on Ubuntu Linux.

Installing the PostgreSQL on your Ubuntu 22.04 Jammy Jellyfish system is straightforward, Now import the PostgreSQL GPG key to verify the authenticity of the installation:

sudo wget -O- https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql.gpg

Then, add the PostgreSQL repository use this command:

echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main | sudo tee /etc/apt/sources.list.d/postgresql.list

After that, install the latest version of PostgreSQL (v14) using the below command:

sudo apt update
sudo apt install postgresql-client postgresql

Once the installation is successful, Postgres 14 will be started. Use this command to check the service status:

sudo systemctl status postgresql

Verify the installation by connecting to the PostgreSQL database server and checking its version. Use this command:

sudo -u postgres psql -c "SELECT version();"

Step 3. Create PostgreSQL Database and Role.

  • Create a new PostgreSQL RoleThe following command will create a new role named meilana:
    sudo su - postgres -c "createuser jelita"
  • Create a new PostgreSQL DatabaseCreate a new database named meilana using the createdb command:
    sudo su - postgres -c "createdb mega"
  • Grant privilegesTo grant permissions to the ramona user on the database we created in the previous step, connect to the PostgreSQL shell:
    sudo -u postgres psql

    and run the following query:

    grant all privileges on database jelita to mega;

Step 4. Configure UFW Firewall.

Set up a UFW firewall rule to allow PostgreSQL to communicate on its default port 5432, which is required if you plan to use anything else besides localhost:

sudo ufw enable
sudo ufw allow proto tcp from (you-ip-address) to any port 5432

That’s all you need to do to install PostgreSQL on Ubuntu 22.04 LTS Jammy Jellyfish. I hope you find this quick tip helpful. For further reading on the installation of the PostgreSQL database, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.