Never knowingly knowing narwhals

Mac OSX: starting postgres on boot

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
 

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 start

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 :)

5 Comments to Mac OSX: starting postgres on boot

  1. July 10, 2008 at 5:03 pm | Permalink

    Excellent. Thanks so much.

  2. July 15, 2008 at 3:10 pm | Permalink

    Thank you, this really helped. Postgres was working for me until I reinstalled Leopard. The only issue I had was the fact that the PostgreSQL/PostgreSQL had the wrong permissions when I ran the SystemStarter. You might want to add this to your directions:

    chmod +x PostgreSQL/PostgreSQL

    Other than that, everything worked perfectly.

  3. Saint's Gravatar Saint
    May 3, 2010 at 12:02 pm | Permalink

    Worked like a charm! Thanks heaps!!

  4. Carl's Gravatar Carl
    November 29, 2010 at 5:18 pm | Permalink

    Heya! Thanks much for this. This seems to be one of the clearest explanations on Mac OS X PostgreSQL startup out there.

    Would you mind if I link to this page from the PostgreSQL docs (9.0) on server startups?

    Also: I too had the permissions error that Mike mentions. Even after his fix, sudo SystemStarter -n -D was reporting a failure to start (“execution of Startup script failed”). When I did restart anyway, however, postgres was running fine.

  5. Andre's Gravatar Andre
    September 4, 2011 at 10:12 pm | Permalink

    It works like a charm. Thank you very much. Just one thing: the script only works for starting the service up, not shutting it down or restarting it.

    So I would change the bash script bit a little. I’d replace the word start at the end of the line for $1 . That way you can actually using the SystemStarter tool to start and stop the service (e.g., SystemStarter stop PostgreSQL)

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>