09-08-2023, 01:08 PM
Before proceeding, ensure your virtual private server (VPS) runs Ubuntu 20.04 or later.
Option 1 – Installation From APT Repository
The popular method to install PostgreSQL on Ubuntu is from the official repository via the APT package manager. Here are the steps to do so:
- Run these commands to import the key from PostgreSQL’s official repository to the APT package manager:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg - - Refresh APT to synchronize all repositories using this command:
sudo apt update - Install the latest version of PostgreSQL with APT and sudo privileges:
sudo apt-get -y install postgresql - To confirm the installation, run this command:
sudo -u postgres psql - Enter the PostgreSQL version query statement:
SELECT version();
Option 2 – Installation Using Local Ubuntu Repository
The alternative PostgreSQL installation method uses the local Ubuntu repository. Here are the steps:
- Run this command to refresh the Ubuntu repository:
sudo apt-get update - Verify the available PostgreSQL apt repository using this command:
sudo apt show postgresql - Run this command to install the latest available version. In our case, it is version 12.15:
sudo apt install postgresql postgresql-contrib - To verify whether the PostgreSQL installation is successful, run the following:
sudo -u postgres psql - Then, run this command to check the current PostgreSQL version:
SELECT version();
The command line should show that the PostgreSQL server version 12.15 is successfully installed, similar to this:


