You are here: Home Tech Postgres startup on OS X 10.6

Postgres startup on OS X 10.6

by Chris Shenton last modified Nov 21, 2011 07:23 PM
Use launchd and launchctl to start our hand-built Postgres server on OS X 10.6 Snow Leopard.

Postgres built by hand

I build postgres 8.4.2 by hand with a typical CMMI:

./configure --with-openssl

For this application I didn't need Perl or Python or other stored procedures and options. I probably should have added a couple others like:

--with-bonjour

It installed in the default location, /usr/local/pgsql.

Then followed the install guides about creating a 'postgres' user, a data dir, changing ownerships, running initd.

sysctl.conf

I had to increase some kernel values to get initdb to run; in /etc/sysctl.conf:

kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.sysv.shmall=65536
kern.sysv.shmmax=16777216

In order to avoid rebooting, I set these on the live system by using the command:

sudo sysctl -w $VARIABLE=$VALUE

for each variable and value above.

launchd

I'm an old FreeBSD/SunOS/Solaris/Linux UNIX guy and launchd is new to me. I eventually pieced together this startup file for OS X. In /Library/LaunchDaemons/org.postgresql.postmaster.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
          "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>org.postgresql.postmaster</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/pgsql/bin/postgres</string>
      <string>-D/usr/local/pgsql/data</string>
      <string>--logging_collector=YES</string>
      <string>--log_connections=on</string>
      <string>--log_directory=/usr/local/pgsql/log</string>
      <string>--log_filename=postgres_%Y-%m-%d_%H:%M.log</string>
      <string>--log_rotation_size=1024</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>UserName</key>
    <string>postgres</string>
    <key>KeepAlive</key>
    <dict>
      <key>SuccessfulExit</key>
      <false/>
    </dict>
  </dict>
</plist>

In the above I have it logging to a datetime-stamped file which rotates after it gets to be 1MB in size. Remember to create that log dir and make it writable by your postgres user.

Load and go

To get the system to know about it, load it:

sudo launchctl load /Library/LaunchDaemons/org.postgresql.postmaster.plist

It should start automatically. If you've got problems (e.g., bad option switches), it won't start; check /var/log/system.log for complaints.

To stop it, use the label name you gave it in the .plist file (which for sanity, should be the same as the plist file itself, sans suffix):

sudo launchctl stop org.postgresql.postmaster
Share this: