In this blog post, I'll show you how to run multiple instances of the same PostgreSQL version on RHEL/CentOS 7/8 and Fedora.

This blog post assumes that PostgreSQL is installed using the PostgreSQL RPM repository as described in here.
All examples are for v13, but it will work for all supported PostgreSQL releases.

Run all of the commands as root.

First, you will need to create a new unit file for the new cluster. You can give any name in here. To distinguish the cluster, I added port value to the unit file name:

cp /lib/systemd/system/postgresql-13.service /etc/systemd/system/postgresql-13-5413.service


Ok, now please edit this file, and change PGDATA. Please note that you can give any directory name in here that postgres can access. You don't have to create this directory now, it will be created in the next step:

Environment=PGDATA=/var/lib/pgsql/13/data-5413


Even though you don't have to, I suggest you to change the Description= part as well, to distinguish the v13 clusters more.

Let's move forward with initdb:

postgresql-13-setup initdb postgresql-13-5413


As you probably noticed, we passed a string to setup script, as a second parameter. This one must match the name of the unit file you created in the previous step. This script will run initdb in the specifled location.

Final step before starting the cluster is changing the port number:

sed -i "s/#port = 5432/port = 5413/g" /var/lib/pgsql/13/data-5413/postgresql.conf


Now, you can start the cluster:

systemctl enable --now postgresql-13-5413.service


This command also enables this service to start after a reboot.

You can check the status of the cluster with the following command:

systemctl status postgresql-13-5413.service



● postgresql-13-5413.service - PostgreSQL 13 database server
Loaded: loaded (/etc/systemd/system/postgresql-13-5413.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-02-12 00:17:42 GMT; 3s ago
Docs: https://www.postgresql.org/docs/13/static/
Process: 1861621 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 1861626 (postmaster)
Tasks: 8 (limit: 18899)
Memory: 16.0M
CPU: 25ms
CGroup: /system.slice/postgresql-13-5413.service
├─1861626 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data-5413
├─1861627 postgres: logger
├─1861629 postgres: checkpointer
├─1861630 postgres: background writer
├─1861631 postgres: walwriter
├─1861632 postgres: autovacuum launcher
├─1861633 postgres: stats collector
└─1861634 postgres: logical replication launcher

Feb 12 00:17:42 postgresql.is.the.best systemd[1]: Starting PostgreSQL 13 database server...
Feb 12 00:17:42 postgresql.is.the.best postmaster[1861626]: 2021-02-12 00:17:42.200 GMT [1861626] LOG: redirecting log output to logging collector process
Feb 12 00:17:42 postgresql.is.the.best postmaster[1861626]: 2021-02-12 00:17:42.200 GMT [1861626] HINT: Future log output will appear in directory "log".
Feb 12 00:17:42 postgresql.is.the.best systemd[1]: Started PostgreSQL 13 database server.



No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.