Automatically Start PostgreSQL on Mac OS X

Why bother remembering very long commands?

There is a shell script for automatically starting, stopping and restarting PostgreSQL if you have installed PostgreSQL on /usr/local. In fact there is a shell script for nearly every redundant task for developers and Linux system administrators (even spammers).

See the following guide first:

PostgreSQL on Mac OS X

The shell script:


#!/bin/bash

start()
{
        echo -n "Starting PostgreSQL server"
        sudo su postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/postgresql.log start'
        
        return
}

stop()
{
        echo -n "Stopping PostgreSQL server"
        sudo su postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ stop'
        return
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: {start|stop|restart}"
        exit 1
        ;;
esac

Usage: /path/to/script start (stoprestart).

You can create aliases on .bash_profile to make the command shorter.

A good friend also has a very nice post for Mac OS users who are also Ruby programmers. I am not sure if we all have the same preferences. Do we use Firefox and Firebug that often? Chrome is worth trying, right? Do we use MongoDB for Rails apps? You can rework his script according to your needs. Check out the post of Radamanthus Batnag on how to automatically start applications you need to get ready for “work mode.”