12

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?

asked May 28, 2021 at 16:53

4 Answers 4

8

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
answered Mar 7, 2022 at 11:45
3
  • I assume that this does not work for the first installation of PostgreSQL? Since there is no postgres user before the initdb after installation. Is this true? If so, how can one specify cluster location right after the first installation of PostgreSQL? Commented Jan 19, 2023 at 13:07
  • 1
    It does work, at least for postgresql14 on RH7 - the user postgres is present before running initdb Commented Jul 11, 2023 at 19:45
  • Works for pg 9 and 11 Commented Jan 3 at 11:44
1

I've found the easiest way to move the database to its own drive is to move the postgres home folder prior to creating the db.

mv /var/lib/pgsql /home/postgres
usermod -d /home/postgres postgres
sed -i 's|/var/lib/pgsql|/home/postgres|g' /usr/lib/systemd/system/postgresql.service
sed -i 's|/var/lib/pgsql|/home/postgres|g' /usr/bin/postgresql-setup
systemctl daemon-reload
/usr/bin/postgresql-setup --initdb

While most of the commands are relatively self-explanatory, the sed -i 's|x|y|g' file is a simple one-line find x/replace with y using sed command.

What's sad is that even though the /usr/bin/postgresql-setup script has POSTGRES_HOMEDIR=/var/lib/pgsql variable to define the home, it still has /var/lib/pgsql hard-coded in other places. Even sadder that the script does not just read the configured home from /etc/passwd file.

answered Feb 22, 2024 at 20:04
0

/usr/pgsql-12/bin/initdb --pgdata=/data/pgsql

answered Jun 8, 2021 at 22:22
0

postgresql-setup doesn't create systemd service file.

You must create it from scratch /lib/systemd/system/postgresql-14.service or create config file for default systemd service file.

For example /lib/systemd/system/db-test.service, which contain path to desired db

# Location of database directory
Environment=PGDATA=/data/pgsql/db_test

then run

# systemctl daemon-reload
# systemctl db-test.service
# /usr/bin/postgresql-14-setup initdb db-test
Initializing database ... OK

and voilà

# ls -l /data/pgsql/db_test/pg_hba.conf 
-rw------- 1 postgres postgres 4577 Feb 25 15:19 /data/pgsql/db_test/pg_hba.conf

If you want use default service then create config /lib/systemd/system/postgresql-14.service.d/postgresql-14.conf for service /lib/systemd/system/postgresql-14.service

For example

# cat /lib/systemd/system/postgresql-14.service.d/postgresql-14.conf
[Service]
Environment=PGDATA=/data/pgsql/postgresql-14
answered Feb 25, 2022 at 8:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.