Install PostgreSQL on Mac Lion via Homebrew

First install homebrew if you haven’t already:

Now install postgresql via homebrew

brew update && brew upgrade
brew install postgresql

Wait for everything to finish.

Create postgreSQL database:

initdb /usr/local/var/postgres

Fix conflict from Mac’s own postgreSQL:

BREW_POSTGRES_DIR=`brew info postgres | awk '{print $1"/bin"}' | grep "/postgresql/"`
LION_POSTGRES_DIR=`which postgres | xargs dirname`
LION_PSQL_DIR=`which psql | xargs dirname`
sudo mkdir -p $LION_POSTGRES_DIR/archive
sudo mkdir -p $LION_PSQL_DIR/archive
for i in `ls $BREW_POSTGRES_DIR`
    if [ -f $LION_POSTGRES_DIR/$i ]
        sudo mv $LION_POSTGRES_DIR/$i $LION_POSTGRES_DIR/archive/$i
        sudo ln -s $BREW_POSTGRES_DIR/$i $LION_POSTGRES_DIR/$i
    if [ -f $LION_PSQL_DIR/$i ]
        sudo mv $LION_PSQL_DIR/$i $LION_PSQL_DIR/archive/$i
        sudo ln -s $BREW_POSTGRES_DIR/$i $LION_PSQL_DIR/$i

Also, make sure /usr/local/bin comes first in your /etc/paths. Run “brew doctor” to check if there is any path issue.

You need to fix those issues before install bundled stuff such as rails pg gem (if not, use “gem uninstall pg && gem install pg” to reinstall pg gem).

Start service:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

To stop service:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

To make postgreSQL auto start:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.1.3/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

To remove this auto start:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

To upgrade from old version of this auto start (first remove auto start see previous section):

cp /usr/local/Cellar/postgresql/NEW.VERSION.ID/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

To Install adminpack (so pgAdmin stop complaining):

psql -d postgres

Also, download pgAdmin:

1 thought on “Install PostgreSQL on Mac Lion via Homebrew”

  1. thanks! took me awhile to work around the preloaded Lion postgres, but finally got it working…great notes

Leave a Reply