Tutorial Install Mutiple PostgreSQL Versions on Linux Ubuntu v24.04


Download PostgreSQL for Linux on https://www.postgresql.org/ftp/source/

In this case I use Linux Ubuntu v24.04

I already download 10 files, and these the plan :

postgresql-9.6.24.tar.gz will has port 5409

postgresql-10.23.tar.gz will has port 5410

postgresql-11.22.tar.gz will has port 5411

postgresql-12.22.tar.gz will has port 5412

postgresql-13.23.tar.gz will has port 5413

postgresql-14.22.tar.gz will has port 5414

postgresql-15.17.tar.gz will has port 5415

postgresql-16.13.tar.gz will has port 5416

postgresql-17.9.tar.gz will has port 5417

postgresql-18.3.tar.gz will has port 5418


Step-1 :   Create directory

$ sudo mkdir -p /usr/local/postgresql

and then

$ cd /usr/local/postgresql


Step-2 :   Move files above into /usr/local/postgresql

$ sudo mv /home/ubuntu/Downloads/postgresql-9.6.24.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-10.23.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-11.22.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-12.22.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-13.23.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-14.22.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-15.17.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-16.13.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-17.9.tar.gz .

$ sudo mv /home/ubuntu/Downloads/postgresql-18.3.tar.gz .


Step-3 :   Extract file .tar.gz

$ sudo tar xzf ./postgresql-9.6.24.tar.gz

$ sudo rm -rf ./postgresql-9.6.24.tar.gz

$ sudo mv postgresql-9.6.24 postgresql-09

$ cd postgresql-09


Step-4 :   Prerequisites

Before starting, ensure your system has the necessary build tools.

$ sudo apt update

$ sudo apt install -y build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev libperl-dev python3-dev


Step-5 :   Create a dedicated user

$ sudo adduser --system --group postgres


Step-6 :   Configure the build

$ ./configure --prefix=/usr/local/postgresql/postgresql-09


Step-7 :   Compile and install

$ sudo make

$ sudo make install


Step-8 :   Initialize and configure ports

$ sudo mkdir -p /usr/local/postgresql/postgresql-09/data

$ sudo chown postgres:postgres /usr/local/postgresql/postgresql-09/data


Step-9 :   Set ownership role on Linux

$ sudo chown -R postgres .

$ sudo chgrp -R postgres .


Step-10 :   Initialize database (as the postgres user)

$ sudo su - postgres

$ cd /usr/local/postgresql/postgresql-09/bin

$ ./initdb -D /usr/local/postgresql/postgresql-09/data


Step-11 :   Set the port

Edit the configuration file for that specific version.

$ nano /usr/local/postgresql/postgresql-09/data/postgresql.conf

Find the line #port = 5432 and change it to your specific plan (e.g., port = 5409).

$ exit


To manage your PostgreSQL each versions instance using systemctl, we need to create a Unit file that points to your specific installation and data directories.

Since I'm using Ubuntu 24.04, it is crucial that the data directory is already initialized (via initdb) before starting the service.

Step-12 :   Check existing service system on Linux

$ sudo ls -al /etc/systemd/system | grep "postgresql"


Step-13 :   Create the service file

$ sudo touch /etc/systemd/system/postgresql@09.service

$ sudo nano /etc/systemd/system/postgresql@09.service


Paste these into postgresql@09.service :

---   ---   ---

[Unit]

Description=PostgreSQL v9.6.24 database server

After=network.target


[Service]

Type=forking

User=postgres

Group=postgres


# Path to the installation and data directory

Environment=PGDATA=/usr/local/postgresql/postgresql-09/data

RuntimeDirectory=postgresql


# Use the specific binary path for version 9.6

ExecStart=/usr/local/postgresql/postgresql-09/bin/pg_ctl start -D ${PGDATA} -s -w -t 300

ExecStop=/usr/local/postgresql/postgresql-09/bin/pg_ctl stop -D ${PGDATA} -s -m fast

ExecReload=/usr/local/postgresql/postgresql-09/bin/pg_ctl reload -D ${PGDATA} -s


# Ensure the process doesn't get killed too early

TimeoutSec=300


[Install]

WantedBy=multi-user.target

---   ---   ---


Step-14 :   Verify port configuration

Before starting the service, ensure that the configuration file inside your data directory is explicitly set to 5409. Check the file :

$ sudo grep "port =" /usr/local/postgresql/postgresql-09/data/postgresql.conf

If it doesn't say port = 5409, edit it:

sudo nano /usr/local/postgresql/postgresql-09/data/postgresql.conf


Step-15 :   Register and start the service

Run the following commands to reload the systemd manager and activate your new service.

Reload systemd :

$ sudo systemctl daemon-reload

Enable on boot :

$ sudo systemctl enable postgresql@09

Start the service :

$ sudo systemctl start postgresql@09

Check status :

$ sudo systemctl status postgresql@09


Done

Comments