HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

postgresql-setup --initdb with custom data directory

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlinitdbdirectorywithcustomsetupdata

Problem

I am trying to setup Postgres 12 on Centos 8.
Postgres shall be installed in the default directory i.e. /var/lib/pgsql, however I want the data directory to be in /data/pgsql

I want to use postgresql-setup as root, as I believe it will create systemd service files along with it, rather than using pg_ctl or running initdb as postgres user.

However, if I try

$ postgresql-setup --initdb --pgdata=/data/pgsql/


I will receive the following error.

postgresql-setup: unrecognized option '--pgdata=/data/pgsql'
FATAL: can't parse arguments


What is the best way to achieve this?

Solution

If you wish to place your data in a custom directory /pgdata/14/data, create the directory with the correct permissions:
mkdir -p /pgdata/14/data
sudo chown postgres:postgres /pgdata/14/data


Then get systemctl to create a service file:
sudo systemctl edit postgresql-14.service


As mentioned above, just add the PGDATA location:
[Service]
Environment=PGDATA=/pgdata/14/data


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

To check its content:
# cat /etc/systemd/system/postgresql-14.service.d/override.conf
[Service]
Environment=PGDATA=/pgdata/14/data


Reload systemd:
# systemctl daemon-reload


Initialize the PostgreSQL data directory:
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb


Start and enable the service:
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14

Context

StackExchange Database Administrators Q#292431, answer score: 4

Revisions (0)

No revisions yet.