Thursday, June 3, 2021

Installation of Postgresql in RHEL 7/CentOS 7 with different data directory

 By default, on CentOS 7, the PostgreSQL v13 data directory is located in /var/lib/pgsql/13/data.

Here’s a simple trick to easily place it somewhere else without using symbolic links.

First of all, install PostgreSQL 13:
# sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# sudo yum install -y postgresql13-server

If you wish to place your data in (e.g.) /database/pg_data, create the directory with the good rights:

# mkdir -p /database/pg_data
# chown -R postgres:postgres /pgdata

Then, customize the systemd service:

# systemctl edit postgresql-13.service

Add the following content:

[Service]
Environment=PGDATA=/database/pg_data

This will create a /etc/systemd/system/postgresql-13.service.d/override.conf file which will be merged with the original service file.

To check its content:

# cat /etc/systemd/system/postgresql-13.service.d/override.conf
[Service]
Environment=PGDATA=/pgdata/13/data

Reload systemd:

# systemctl daemon-reload

Initialize the PostgreSQL data directory:

# /usr/pgsql-13/bin/postgresql-13-setup initdb

Start and enable the service:

# sudo systemctl enable postgresql-13
# sudo systemctl start postgresql-13

No comments:

Post a Comment