OS X, Homebrew, PostgreSQL: Work around false running report by launchctl

Logos of OS X, PostgreSQL, and Homebrew.

You have PostgreSQL installed with Homebrew on OS X. After you restarted your Mac, somehow Rails or other apps using PostgreSQL will report the server is not running while trying to start it with launchctl will say otherwise.

Turns out, the .plist file used by launchctl is only checking for the existance of the .pid file written by PostgreSQL when starting up. If you don’t shut down PostgreSQL before restarting the computer, that file will stay there. When your Mac starts up again, launchctl will see that .pid file and will assume PostgreSQL is already running while it’s actually not. Annoying, I know.

Solution

  • First, make sure PostgreSQL is actually not running:

    $ ps aux | grep bin/postgres
    

    Maybe except for grep bin/postgres, the above command should display nothing. If that is the case, then PostgreSQL is not running.

  • Unload the launcher with launchctl:

    $ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    
  • Remove the .pid file:

    $ rm /usr/local/var/postgres/postmaster.pid
    
  • Load the launcher with launchctl:

    $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    
  • Check to see if PostgreSQL is running:

    $ ps aux | grep bin/postgres
    

    The above command should now display a process of PostgreSQL running.

That should bring your PostgreSQL server back up, and let Rails or whatever else use it.

Leave a Reply