PostgreSQL won't start after moving datadir on Centos 7

SPRBRN asked:

I’ve installed PostgreSQL 9.3 on Centos 7. After installing I could start the system service and login to psql. I want to move the PGDATA folder to another partition, so I tried several things to change DATADIR.

I copied the /var/lib/pgsql/data folder to /postgresdata/data, then created a symlink:

systemctl stop postgresql
cp -rp /var/lib/pgsql/data /postgresdata/data
mv /var/lib/pgsql/data /var/lib/pgsql/data.old
ln -s /postgresdata/data /var/lib/pgsql/data
systemctl start postgresql

The folder /postgresdata/ is set to 700 and owner postgres.

This results in the following error:

Job for postgresql.service failed because the control process exited with error code. See “systemctl status postgresql.service” and “journalctl -xe” for details.

I tried two other methods as well, both resulting in the same error:

Method 1: Change postgresql.conf

data_directory = '/postgresdata/data'

Method 2: Change the system service setting:

vim /usr/lib/systemd/system/postgresql.service

Then change:

# Environment=PGDATA=/var/lib/pgsql/data

Both result in the same error.

When I run journalctl -xe I get this:

-- Unit postgresql.service has begun starting up.
apr 25 15:08:03 srv001 pg_ctl[15517]: FATAL:  could not open file "/postgresdata/data/PG_VERSION": Permission denied
apr 25 15:08:04 srv001 systemd[1]: postgresql.service: control process exited, code=exited status=1
apr 25 15:08:04 srv001 pg_ctl[15517]: pg_ctl: could not start server
apr 25 15:08:04 srv001 pg_ctl[15517]: Examine the log output.
apr 25 15:08:04 srv001 systemd[1]: Failed to start PostgreSQL database server.
-- Subject: Unit postgresql.service has failed
-- Defined-By: systemd
-- Unit postgresql.service has failed.

Looking at /postgresdata/data/PG_VERSION I see no problems with rights. They’re identical to the original it seems.

How can I move the data-folder for PostgreSQL on Centos 7?

My answer:

The easiest thing to do is to mount your new disk at /var/lib/pgsql and save yourself a lot of trouble. This way you don’t have to worry about altering SELinux policies or any of that.

If you insist on putting PostgreSQL’s data directory in an unexpected location, you can make SELinux work correctly by setting a new file context for the new directory structure that is equivalent to the existing one. For example:

semanage fcontext --add --equal /var/lib/pgsql /postgresdata

This causes security contexts under /postgresdata to be applied as if they were under /var/lib/pgsql. You can then fix the contexts of any existing files with restorecon:

restorecon -rv /postgresdata

Be aware that people are not going to understand why you did this. That includes any admins that look at this server in the future, including yourself. There’s no need to use a nonstandard directory simply because you are using a new disk. Just mount the new storage at /var/lib/pgsql.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.