Installing PostgreSQL from source, and running two versions on one server in Centos 8
PostgreSQL is a free object-relational database management system, the most advanced open source database management system and is a real alternative to commercial databases. PostgreSQL is based on the SQL language.
Training
Installing the required packages
$ sudo dnf -y install gcc make readline-devel zlib-devel systemd-devel
Create the postgres system user
$ sudo useradd -M -s -d /var/lib/postgresql postgres
Create a directory for PostgreSQL logs and assign rights
$ sudo mkdir /var/log/postgresql
$ sudo chown -R postgres:postgres /var/log/postgresql
Installing PostgreSQL 9.6 from source, running on port 5433
Download the PostgreSQL 9.6 archive to the / tmp directory and unzip it
$ wget https://ftp.postgresql.org/pub/source/v9.6.19/postgresql-9.6.19.tar.gz -P /tmp
$ cd /tmp
$ tar xzf postgresql-9.6.19.tar.gz
$ cd postgresql-9.6.19
Launching PostgreSQL 9.6 Configuration
$ sudo ./configure --with-systemd --prefix=/opt/postgresql/9.6
Install PostgreSQL 9.6
$ sudo make
$ sudo make install
Create a directory for the PostgreSQL 9.6 database and assign rights
$ sudo mkdir -p /var/lib/postgresql/9.6/main
$ sudo chown -R postgres:postgres /var/lib/postgresql
Switching to postgres user
$ sudo su - postgres
Initialize the database and close the postgres user session
$ /opt/postgresql/9.6/bin/initdb -D /var/lib/postgresql/9.6/main
$ exit
Editing the PostgreSQL 9.6 config, changing the default port to 5433
$ sudo nano /var/lib/postgresql/9.6/main/postgresql.conf
[...]
listen_addresses="*"
port = 5433
Starting PostgreSQL 9.6
$ sudo su - postgres -c "/opt/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main/ -l /var/log/postgresql/postgresql-9.6.log start"
Check if port 5433 is available
$ ss -nltup | grep 5433
tcp LISTEN 0 128 0.0.0.0:5433 0.0.0.0:*
tcp LISTEN 0 128 [::]:5433 [::]:*
Checking the connection to the DBMS
$ sudo su - postgres
$ export PGPORT=5433
$ /opt/postgresql/9.6/bin/psql
See version
=# SELECT version();
PostgreSQL 9.6.19 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
We leave
=# q
$ exit
Stop PostgreSQL 9.6
$ sudo su - postgres -c "/opt/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main/ stop"
For the convenience of starting the service, create a Systemd Unit for Postgresql 9.6
$ sudo nano /etc/systemd/system/postgresql-9.6.service
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=notify
User=postgres
Group=postgres
# Location of database directory
Environment=PGDATA=/var/lib/postgresql/9.6/main/
ExecStart=/opt/postgresql/9.6/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
Add the service to startup and check the status
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now postgresql-9.6
$ systemctl status postgresql-9.6
Opening port 5433 in Firewalld
$ sudo firewall-cmd --permanent --zone=public --add-port=5433/tcp
$ sudo firewall-cmd --reload
Installing PostgreSQL 11 from source, running on port 5432
Download the PostgreSQL 11 archive to the / tmp directory and unzip it
$ wget https://ftp.postgresql.org/pub/source/v11.7/postgresql-11.7.tar.gz -P /tmp
$ cd /tmp
$ tar xzf postgresql-11.7.tar.gz
$ cd postgresql-11.7
Launching PostgreSQL 11 Configuration
$ sudo ./configure --with-systemd --prefix=/opt/postgresql/11
Install PostgreSQL 11
$ sudo make
$ sudo make install
Create directory for PostgreSQL 11 database and assign rights
$ sudo mkdir -p /var/lib/postgresql/11/main
$ sudo chown -R postgres:postgres /var/lib/postgresql
Switching to postgres user
$ sudo su - postgres
Initialize the database and close the postgres user session
$ /opt/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main
$ exit
Editing the PostgreSQL 11 config
$ sudo nano /var/lib/postgresql/11/main/postgresql.conf
[…]
listen_addresses="*"
port = 5432
Starting PostgreSQL 11
$ sudo su - postgres -c "/opt/postgresql/11/bin/pg_ctl -D /var/lib/postgresql/11/main/ -l /var/log/postgresql/postgresql-11.log start"
Check if port 5432 is available
$ ss -nltup | grep 5432
tcp LISTEN 0 128 0.0.0.0:5432 0.0.0.0:*
tcp LISTEN 0 128 [::]:5432 [::]:*
Checking the connection to the DBMS
$ sudo su - postgres
$ /opt/postgresql/11/bin/psql
See version
=# SELECT version();
PostgreSQL 11.7 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
We leave
=# q
$ exit
Stop PostgreSQL 11
$ sudo su - postgres -c "/opt/postgresql/11/bin/pg_ctl -D /var/lib/postgresql/11/main/ stop"
For the convenience of starting the service, create a Systemd Unit for Postgresql 11
$ sudo nano /etc/systemd/system/postgresql-11.service
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=notify
User=postgres
Group=postgres
# Location of database directory
Environment=PGDATA=/var/lib/postgresql/11/main/
ExecStart=/opt/postgresql/11/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
Add the service to startup and check the status
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now postgresql-11
$ systemctl status postgresql-11
Opening port 5432 in Firewalld
$ sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
$ sudo firewall-cmd --reload