How to run postgres on centos when installed via YUM repo as default daemon user

19,249

Solution 1

This is only for a fresh installation (as it pertained to my situation) as it involves blowing away the data dir.

The steps I took to resolve this issue while utilizing the packaged startup scripts for a fresh installation:

  1. Remove the postgres data dir /var/lib/pgsql/9.2/data if you've already gone through the initdb process with the postgres user:group configured as default.
  2. Modify the startup script (/etc/init.d/postgresql-9.2) to replace all instances of postgres:postgres with NEWUSER:NEWGROUP.
  3. Modify the startup script to replace all instances of postgres in any $SU -l postgres lines with the NEWUSER.
  4. run /etc/init.d/postgres initdb to regenerate the cluster using the new username
  5. Make sure any logs created are owned by the new user or remove old logs if error on initdb (the configuration file in my case was found in /var/lib/pgsql/9.2/data/postgresql.conf).
  6. Startup postgres and it should now be running under the new user/group.

I understand this might not be what other people are looking for if they have existing postgres db's and want to restart the server to run as a different user/group combo - this was not my case, and I didn't see an answer posted anywhere for a 'fresh' install utilizing the pre-packaged startup scripts.

Solution 2

In addition to AndrewPK's explanation, I'd like to note that you can also start new PostgreSQL instances as any user by stopping and disabling the system Pg service, then using:

initdb -D /path/to/data/directory
pg_ctl start -D /path/to/data/directory

This won't auto-start the server on boot, though. For that you must integrate into your init system. On CentOS 6 a simple System V-style init script in /etc/init.d/ and a suitable symlink into /etc/rc3.d/ or /etc/rc3.d/ (depending on default runlevel) is sufficient.

If running more than one instance at a time they must be on different ports. Change the port directive in postgresql.conf in the datadir or set it on startup with pg_ctl -o "-p 5433" .... You may also need to override the unix_socket_directories if your user doesn't have write permission to the default socket directory.

Share:
19,249

Related videos on Youtube

AndrewPK
Author by

AndrewPK

I lurk mostly...mostly.

Updated on September 15, 2022

Comments

  • AndrewPK
    AndrewPK over 1 year

    With a freshly installed version of Postgres 9.2 via yum repository on Centos 6, how do you run postgres as a different user when it is configured to run as 'postgres:postgres' (u:g) out of the box?

    • AndrewPK
      AndrewPK
      If you're constrained to the users/groups you can create on a box - as I was. Enterprise "security" is fun!
  • AndrewPK
    AndrewPK over 11 years
    Before running initdb you would need to su/login as the user as well. Great tip!
  • akhmed
    akhmed almost 9 years
    Note that for psql 9.4 you will get pg_ctl: command not found since pg_ctl is no longer located in the PATH. You have to manually navigate to /usr/lib/postgresql/9.4/bin/ (on Ubuntu). Both initdb and pg_ctl should be there. And then this solution works perfectly for 9.4 as well.