I'm having trouble getting Postgres 9.4 working on a clean installation of CentOS 7 (fully patched) when using a non-default data directory.
It's taken me a while to get here using various articles but now I'm stumped. The directory I want the database in is /opt/pgsql/data/ Here's what I've done:
disable SELinux and reboot
yum install postgresql94-server postgresql94-contrib pgadmin3_94
sudo mkdir -p /opt/pgsql/data
sudo chown -R postgres:postgres /opt/pgsql/
sudo chmod -R 700 /opt/pgsql/
sudo cp /usr/lib/systemd/system/postgresql-9.4.service /etc/systemd/system/postgresql-9.4.service
sudo vi /etc/systemd/system/postgresql-9.4.service
change Environment=PGDATA=/var/lib/pgsql/9.4/data/ to Environment=PGDATA=/opt/pgsql/data
sudo systemctl daemon-reload
sudo vi /var/lib/pgsql/.pgsql_profile
add: PGDATA=/opt/pgsql/data
The next command is the one that fails:
sudo su - postgres -c '/usr/pgsql-9.4/bin/postgresql94-setup initdb'
It throws this error: Initializing database ... failed, see /var/lib/pgsql/9.4/initdb.log
Looking in the log file I see this: runuser: may not be used by non-root users
I've tried su-ing to postgres with the same result (e.g. sudo su - postgres -c '/usr/pgsql-9.4/bin/postgresql94-setup initdb')
I've also checked the $PGDATA variable is correctly set when becoming the 'postgres' user.
Any pointers would be very welcome.