Postgresql – runuser error on CentOS 7 Postgres 9.4

postgresql

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.

Best Answer

The /usr/pgsql-9.4/bin/postgresql94-setup script needs to be executed as root.

It detects the data directory parsing the service file.

This is the full command help:

Usage: postgresql94-setup {initdb|upgrade} [SERVICE_NAME]

Script is aimed to help sysadmin with basic database cluster administration.

The SERVICE_NAME is used for selection of proper unit configuration file; For
more info and howto/when use this script please look at the docu file
$README_RPM_DIST.  The 'postgresql'
string is used when no SERVICE_NAME is explicitly passed.

Available operation mode:
  initdb        Create a new PostgreSQL database cluster.  This is usually the
                first action you perform after PostgreSQL server installation.
  upgrade   Upgrade PostgreSQL database cluster to be usable with new
                server.  Use this if you upgraded your PostgreSQL server to
                newer major version (currently from $PREVMAJORVERSION \
to $PGMAJORVERSION).

Environment:
  PGSETUP_INITDB_OPTIONS     Options carried by this variable are passed to
                             subsequent call of \`initdb\` binary (see man
                             initdb(1)).  This variable is used also during
                             'upgrade' mode because the new cluster is actually
                             re-initialized from the old one.
  PGSETUP_PGUPGRADE_OPTIONS  Options in this variable are passed next to the
                             subsequent call of \`pg_upgrade\`.  For more info
                             about possible options please look at man
                             pg_upgrade(1).
  PGSETUP_DEBUG              Set to '1' if you want to see debugging output.