Configure non-default PostgreSQL instance for automatic start on machine start in RHEL

Due to policies for Red Hat family distributions, the PostgreSQL installation is not enabled for automatic start or have the database initialized automatically. Database cluster need to be initialized manually post installation. Once initialized, data directory will get created but still PostgreSQL will not be running. We can use ‘systemctl’ service to enable and start postgresql-<versionnumber> service.

For the default instance it can be achieved using below steps mentioned in PostgreSQL installation instruction https://www.postgresql.org/download/linux/redhat/

sudo systemctl enable postgresql-16

In order to make non default instance start automatically after OS restart, we need to create a service file using existing postgresql service file (e.g. “postgresql-13.service” file for PostgreSQL13) followed by updating the PGDATA variable value to data directory of non-default instance and once done using systemctl command to enable new service for automatic start at OS start.

In below code a new service file “postgresql-13-5443.service” is getting created by copying existing service file of default instance postgresql-13.service. Here you can name new the service file as per your choice. I named it “postgresql-13-5443.service” as I created non-default instance on port 5443. (please update PostgreSQL version number as per your environment)

Once new service file is created, edit PGDATA value in the newly created service file and enable new service.

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

#Edit PGDATA value in the newly created service file
Environment=PGDATA=/inst1

# Enable new service
systemctl enable postgresql-13-5443.service

Once non-default instances would be configured for automatic start after OS start, you can see their status using systemctl status command.

In below screenshot, you can see that both postgresql-12-5443.service and postgresql-13.service are enabled for automatic start.

Hope you will find this article useful and informative!

Happy learning!!