Install a Specific Version of PostgreSQL on Ubuntu 20.04: Easy Steps
This tutorial intends to teach you to Install a Specific Version of PostgreSQL on Ubuntu 20.04. To install a specific version of PostgreSQL on your server, you must add the repository and then install it. Here on the Orcacore website, we will show you to set up the latest PostgreSQL version which is PostgreSQL 16 on Ubuntu 20.04. PostgreSQL 16 has improved the performance and more features that you can use for your databases.
Before you start to Install a Specific Version of PostgreSQL on Ubuntu 20.04, you must have access to your server as a non-root user with sudo privileges. To do this, you can follow this guide on Initial Server Setup with Ubuntu 20.04.

Step 1 – Install Required Packages For PostgreSQL Installation
First, you must run the system update with the command below:
sudo apt update
Then, install the required packages that are needed for your PostgreSQL 16 on Ubuntu 20.04:
sudo apt install dirmngr ca-certificates software-properties-common gnupg gnupg2 apt-transport-https curl wget -y
Step 2 – Import PostgreSQL GPG Key and Repository on Ubuntu
To install the latest PostgreSQL, first, create a file repository configuration with the command below:
sudo sh -c 'echo "deb [arch=amd64] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Then, import the GPG key by using the following wget command:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Next, update your system packages:
sudo apt update
Step 3 – Install PostgreSQL 16 on Ubuntu 20.04
At this point, use the command below to install the latest PostgreSQL on your server which is PostgreSQL 16:
sudo apt -y install postgresql
Step 4 – Install a Specific Version of PostgreSQL on Ubuntu
If you want to Install a Specific Version of PostgreSQL on Ubuntu 20.04, you can use the ‘postgresql-14’ or similar instead of ‘postgresql’ in the command. For example:
sudo apt -y install postgresql-14
Step 5 – Enable PostgreSQL Service
By default, PostgreSQL will be activated during the installation. To confirm this, run the following command:
sudo systemctl status postgresql
<strong><mark>Output</mark></strong>
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr>
Active: <strong><mark>active</mark></strong> (<strong><mark>exited</mark></strong>) since Tue 2023-09-19 09:31:02 CEST; 1min 16s ago
Main PID: 10694 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4620)
Memory: 0B
CGroup: /system.slice/postgresql.service
...
If your PostgreSQL service is not activated on Ubuntu 20.04, run the command below to enable it:
sudo systemctl enable postgresql --now
Step 6 – Access PostgreSQL Shell on Ubuntu
PostgreSQL uses a concept called roles to handle authentication and authorization.
During the PostgreSQL installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account.
If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.
One way is to switch over to the Postgres account on your server by running the following command:
su postgres
<strong><mark>Output</mark>
</strong>postgres@olivia:~$
You can access the Postgres shell on Ubuntu 20.04 by running the following command:
postgres@olivia:~$ <strong>psql</strong>
With this command, you will see that your shell prompt changes to:
<strong><mark>Output</mark>
</strong>psql (16.0 (Ubuntu 16.0-1.pgdg20.04+1))
Type "help" for help.
postgres=#
To exit from the PostgreSQL shell on Ubuntu 20.04, run the following command:
postgres=# q
With this command, you will be back to the Postgres Linux command prompt:
postgres@olivia:~$
You can type exit to return to the regular system user:
postgres@olivia:~$ exit
For more information, you can visit the PostgreSQL Documentation page.
Conclusion
At this point, you have learned to Install the latest PostgreSQL on Ubuntu 20.04 which is PostgreSQL 16. Also, if you plan to Install a Specific Version of PostgreSQL on Ubuntu 20.04, you need to specify it in the PostgreSQL installation command.
Hope you enjoy this guide on Install a Specific Version of PostgreSQL on Ubuntu 20.04. You may also interested in these articles:
Install MariaDB 10.11 on Ubuntu 20.04
Set up MySQL and Workbench on Ubuntu 20.04
Reset MySQL or MariaDB Root Password on Ubuntu 20.04
Uninstall MySQL Server from Ubuntu Completely
Alternative Solutions for Installing a Specific Version of PostgreSQL on Ubuntu 20.04
While the provided method using apt
and the PostgreSQL APT repository is straightforward, other approaches exist to achieve the same goal. Here are two alternative methods: using Docker and compiling from source.
1. Installing PostgreSQL with Docker
Docker provides a containerization platform that allows you to run applications in isolated environments. This can be particularly useful for managing different versions of PostgreSQL without conflicts.
Explanation:
Docker images are pre-built environments containing everything needed to run an application, including the operating system, libraries, and dependencies. The official PostgreSQL Docker images are available on Docker Hub and provide a convenient way to deploy specific PostgreSQL versions.
Steps:
- Install Docker: If you don’t have Docker installed, you can install it using the following commands:
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
- Pull the desired PostgreSQL image: Replace
<version>
with the specific PostgreSQL version you want to install (e.g.,14
).
docker pull postgres:<version>
For example, to pull PostgreSQL 14:
docker pull postgres:14
- Run the PostgreSQL container: This command creates and starts a Docker container running the specified PostgreSQL version. You’ll need to set a password for the
postgres
user. Also, map a host port (e.g., 5432) to the container’s port (5432).
docker run --name postgresql_container -e POSTGRES_PASSWORD=your_password -p 5432:5432 -d postgres:<version>
Replace your_password
with a strong password.
- Access the PostgreSQL server: You can now access the PostgreSQL server running in the Docker container using
psql
or any other PostgreSQL client. You’ll need to connect tolocalhost
on port 5432 (or the port you mapped).
psql -h localhost -p 5432 -U postgres -d postgres
You will be prompted for the password you set in the docker run
command.
Advantages:
- Isolation: Each PostgreSQL version runs in its own container, preventing conflicts.
- Reproducibility: Docker images ensure consistent environments across different systems.
- Ease of Use: Docker simplifies the deployment and management of PostgreSQL.
Disadvantages:
- Overhead: Docker containers introduce some overhead compared to running PostgreSQL directly on the host system.
- Learning Curve: Docker has its own set of concepts and commands to learn.
2. Compiling from Source
Compiling PostgreSQL from source offers the greatest control over the installation process and allows for customization. However, it’s also the most complex method.
Explanation:
This approach involves downloading the source code for the desired PostgreSQL version, configuring the build process, and compiling the code into executable binaries.
Steps:
- Install Build Dependencies: Before compiling, you’ll need to install the necessary build tools and libraries.
sudo apt update
sudo apt install build-essential libreadline-dev zlib1g-dev flex bison libpq-dev -y
- Download the Source Code: Visit the PostgreSQL website (www.postgresql.org) and download the source code for the specific version you want. You can use
wget
to download it directly to your server. Replace<version>
with the desired version number (e.g.,14.9
). Also, replace the URL with the correct download link for your desired version.
wget https://ftp.postgresql.org/pub/source/v<version>/postgresql-<version>.tar.gz
tar -xzf postgresql-<version>.tar.gz
cd postgresql-<version>
- Configure and Build: Use the
configure
script to set up the build process, then compile the code usingmake
.
./configure --prefix=/usr/local/pgsql-<version>
make
sudo make install
The --prefix
option specifies the installation directory. Choose a location that includes the version number to avoid conflicts with other PostgreSQL installations.
- Create a User and Data Directory: Create a dedicated user for the PostgreSQL instance and a data directory to store the database files.
sudo useradd postgresql-<version>
sudo mkdir -p /usr/local/pgsql-<version>/data
sudo chown postgresql-<version> /usr/local/pgsql-<version>/data
sudo chmod 700 /usr/local/pgsql-<version>/data
- Initialize the Database Cluster: Use the
initdb
utility to initialize the database cluster. You’ll need to run this as the dedicated PostgreSQL user.
sudo -u postgresql-<version> /usr/local/pgsql-<version>/bin/initdb -D /usr/local/pgsql-<version>/data
- Start the PostgreSQL Server: You can start the server using
pg_ctl
.
sudo -u postgresql-<version> /usr/local/pgsql-<version>/bin/pg_ctl -D /usr/local/pgsql-<version>/data -l logfile start
- Set Environment Variables (Optional): Add the PostgreSQL binaries to your
PATH
environment variable for easier access. You can add the following line to your~/.bashrc
file (or similar):
export PATH=$PATH:/usr/local/pgsql-<version>/bin
Then, source the file:
source ~/.bashrc
Advantages:
- Customization: Full control over the build process and configuration options.
- No Dependencies on Package Managers: Avoids potential conflicts with system package managers.
Disadvantages:
- Complexity: Requires more technical expertise and manual configuration.
- Time-Consuming: Compiling from source can take a significant amount of time.
- Maintenance: You are responsible for managing updates and security patches.
Choosing the right method for Install a Specific Version of PostgreSQL on Ubuntu 20.04 depends on your specific needs and technical expertise. The apt
method is the simplest for most users, while Docker offers isolation and reproducibility. Compiling from source provides the greatest control but requires more effort.