Mac OSX: starting postgres on boot

28 February 2013

NOTE: This is an old article I wrote in July 2008, it's still relevant today. It was originally posted on luckydonkey.com which I am in the process of retiring.

Every bloody operating system has it's own unique way of starting things at boot time. Today I bothered to learn how OS X does it. Here's how I got PostgreSQL to start when my machine is turned on:

What gets started is handled by a program called SystemStarter. On boot it looks through /Library/StartupItems for folders. Inside those folders it looks for a script with the same name as the folder and a plist called StartupParameters.plist.

So as super user:

cd /Library/StartupItems/
mkdir PostgreSQL
touch PostgreSQL/PostgreSQL
touch PostgreSQL/StartupParameters.plist
chmod +x PostgreSQL/PostgreSQL

Now we need to fill in the details. In PostgreSQL/PostgreSQL enter:

#!/bin/sh
sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/data/logfile $1

This is a long line, but it's basically running postgres (via pg_ctl) as the user postgres. You'll need to change this to the whatever user you run postgres as and where ever your install of postgres is. I've used the standard names and directories so it's likely this will work for you too.

Now lets look at the plist. This file contains information for SystemStarter about when to start postgres, what postgres needs and provides and any messages to print to log files:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
        <key>Description</key>
        <string>PostgreSQL</string>
        <key>Messages</key>
        <dict>
                <key>start</key>
                <string>Starting PostgreSQL</string>
                <key>stop</key>
                <string>Stopping PostgreSQL</string>
        </dict>
        <key>OrderPreference</key>
        <string>None</string>
        <key>Provides</key>
        <array>
                <string>PostgreSQL</string>
        </array>
</dict>
</plist>

You can test your new settings by typing sudo SystemStarter -n -D, fix any error messages, reboot and you should have postgres all the time.

The original article had a number of insightful comments, I've included all their corrections but unfortunately not the original comments. Thanks to all those who contributed to make this a better article.

comments powered by Disqus