Jun 8, 2004

PostgreSQL on MacOS X

There are quite a few resources available on the ’Net about compilation and setup of PostgreSQL on a Mac OS X. This O’Reily article, and this one from Apple just to name a few. For me, where both these articles are falling short is in describing how to set Postgres to start automatically when I boot my Mac. Interestingly, I had to read an article about Oracle on a Mac to see how this can be done for Postgres. Naturally, it is very simple. As long as you followed all the instructions in either of above 2 articles (I opted for installing Postgres and dependencies out of Darwinports, as I am a bit repelled by Fink), all you need to do is:
  1. Create a directory /Library/StartupItems/Postgres
  2. Create 2 files in that directory: Postgres and StartupParameters.plist. Note that a startup script must be called the same as a directory!
  3. chmod(1) above files sensibly (700 is a good option) and chown(1) them to be owned by root:wheel.
  4. Put the following into the Postgres startup script:
    #!/bin/sh
    
    #
    # For postmaster startup options, edit $PGDATA/postgresql.conf
    #
    # Note that PGDATA is set in ~${PGUSER}/.profile, don't try to manipulate it here!
    #
    
    . /etc/rc.common
    
    PREFIX=/opt/local
    PGBIN=${PREFIX}/bin
    PGUSER=postgres
    
    StartService ()
    {
        if [ -x ${PGBIN}/pg_ctl ]; then
            ConsoleMessage "Starting PostgreSQL"
            su -l ${PGUSER} -c "[ -d \${PGDATA} ] && exec ${PREFIX}/bin/pg_ctl start -s -w"
        fi
    }
    
    StopService ()
    {
        if [ -x ${PGBIN}/pg_ctl ]; then
            ConsoleMessage "Stopping PostgreSQL"
            su -l ${PGUSER} -c "exec ${PREFIX}/bin/pg_ctl stop -m fast"
        fi  
    }
    
    RestartService ()
    {
        if [ -x ${PGBIN}/pg_ctl ]; then
            ConsoleMessage "Restarting PostgreSQL"
            su -l ${PGUSER} -c "exec ${PREFIX}/bin/pg_ctl restart -s -m fast"
        fi
    }
    
    RunService "$1"
    
    
  5. Puth the following into StartupParameters.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>Description</key>
    <string>PostgreSQL 7.4.1 Database Server</string>
    <key>Provides</key>
    <array>
    <string>PostgreSQL 7.4.1 Database</string>
    </array>
    <key>Requires</key>
    <array>
    <string>Disks</string>
    </array>
    <key>Uses</key>
    <array>
    <string>Disks</string>
    <string>Network</string>
    <string>NFS</string>
    </array>
    <key>OrderPreference</key>
    <string>Late</string>
    </dict>
    </plist>
You can now try and run sudo /Library/StartupItems/Postgres/Postgres start to check if it all works.

2 comments:

  1. There are start up scripts for the Mac that come with PostgreSQL, check out contrib/start-scripts/, you'll find two files: PostgreSQL.darwin and StartupParameters.plist.darwin. Rename them both to drop the darwin and put them in the startup directory you mentioned. This has worked fine for me on Mac OS X 10.3.4.

    http://developer.postgresql.org/cvsweb.cgi/pgsql-server/contrib/start-scripts/

    ReplyDelete
  2. Hey! Looks like it is worth checking out /contrib directory every now and then, instead of inventing the wheel :)

    ReplyDelete