Changing the default PostgreSQL data folder (PGDATA)

Installing the PostgreSQL server on RHEL, CentOS, Scientific Linux or Fedora installs the PostgreSQL databases and configuration files in “/var/lib/pgsql/data”.

This may or may not be desirable. Let’s assume for a moment you have a separately crafted partition for PostgreSQL to use, let’s say a RAID10 volume. You’d want to change this.

Change the defaults

Use your favorite text editor, in my case nano to create the following file (must be the same as the name of the service)

# nano /etc/sysconfig/pgsql/postgresql

Add the following

PGDATA=/postgresql/data

Optionally you can also add the following to change the default port (example is the default port)

PGPORT=5432

Adjusting SELinux to permit the new data folder (pgdata) location

Should the following command output “Permissive” or “Disabled” then you may skip the details for SELinux.

# getenforce

Run the semanage command to add a context mapping for /opt/postgresql and any other directories/files within it.

# semanage fcontext -a -t postgresql_db_t "/postgresql/data(/.*)?"

Now use the restorecon command to apply this context mapping to the running system

# restorecon -Rv /postgresql/data

Starting PostgreSQL

# chkconfig --levels 345 postgresql on
# service postgresql initdb
# service postgresql start

You’re all set to go! Keep in mind that PostgreSQL listens to ‘localhost’ by default. To change this you need to alter the “listen_address” parameter in “/var/lib/pgsql/data/postgresql.conf” (change will require restart).

// CrashMAG

4 thoughts on “Changing the default PostgreSQL data folder (PGDATA)”

  1. This doesn’t work. First off there is no /etc/sysconfig/pgsql/postgresql file on my system. It was never there.

    I manually edited /etc/init.d/postgresql so PGDATA is set to /db/postgres/data. Then I created it with mkdir, change the file permissions to 0700 and the ownership to postgres:postgres.

    Next step is the semanage command which put a line in /etc/selinux/targeted/contexts/files/file_contexts.local that looks like the /var/lib/pgsql/data line but with my directory instead. Followed by the restorecon command. So far, so good.

    When I ran service postgresql initdb it fails after creating the pg_log directory but the context is unconfined_u:object_r:postgresql_db_t:s0 not system_u:…

    How do I get it to set the context correctly?

    1. Hi Edward,

      What you write first suggest that you’re not using the distributions listed in my example, or a very old version. So at first it seems that your premise is not correct.

      So my best advice would be to launch a VM with Scientific Linux or CentOS, go through the procedure there. Observe that it is working. Figure out the differences between your system and the VM. And apply them. Good luck.

      // CrashMAG

    2. It is not there by default, thus why the instructions say to create it. I assure you, it does work.

Leave a Reply

Your email address will not be published. Required fields are marked *

*