Running Postgres as Launch Daemon

daemonslaunchdpostgresql

I am working on setting up a psql server and would therefore like postgres to run whenever the computer is on, regardless of whether I'm logged it. (The machine is running OS X Yosemite.) I set up a launch daemon to do this, but it doesn't work. syslog gives the following error messages:

Jul 14 17:43:01 user@server sudo[276]:     jaia : TTY=ttys000 ; 
PWD=/Users/jaia ; USER=root ; COMMAND=/usr/bin/nano /Library/LaunchDaemons/homeb
rew.mxcl.postgresql.plist
Jul 14 17:44:35 localhost com.apple.xpc.launchd[1] (homebrew.mxcl.postgresql): T
his service is defined to be constantly running and is inherently inefficient.
Jul 14 17:44:44 localhost com.apple.xpc.launchd[1] (homebrew.mxcl.postgresql): S
ervice only ran for 9 seconds. Pushing respawn out by 1 seconds.
Jul 14 17:44:45 localhost com.apple.xpc.launchd[1] (homebrew.mxcl.postgresql): S
ervice only ran for 0 seconds. Pushing respawn out by 10 seconds.

After that, the last message repeats indefinitely.

Here's my .plist file. The reason for sudo -u _postgres is that postgres can't run as root, which launch daemons do by default.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>homebrew.mxcl.postgresql</string>
  <key>ProgramArguments</key>
  <array>
    <string>sudo</string>
    <string>-u</string>
    <string>_postgres</string>
    <string>/usr/local/bin/pg_ctl</string>
    <string>-D</string>
    <string>/usr/local/var/postgres</string>
    <string>start</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>_postgres</string>
  <key>WorkingDirectory</key>
  <string>/usr/local/var/postgres</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/postgres/server.log</string>
</dict>
</plist>

MORE INFORMATION: Loading the job manually via ssh gives the ungrammatical error message "Could not find domain for". Loading it on the machine itself gives "/Library/LaunchDaemons/homebrew.mxcl.postgresql.plist: File exists".

What could be going wrong here?

Best Answer

Leave out the sudo, -u and _postgres in ProgramArguments. launchd takes care of this with the UserName key.