Brew Install Postgresql on OS X Lion

The Problem

If you’re installing postgresql via homebrew on Lion you might be encountering errors like these:

psql: could not connect to server: Permission denied
	Is the server running locally and accepting
	connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
createuser: could not connect to database postgres: could not connect to server: Permission denied
	Is the server running locally and accepting
	connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

The Cause

Lion comes with a version of postgres already installed and uses those binaries by default. In general you can get around this by using the full path to the homebrew postgres binaries but there may be still issues with other programs.

The Solution

If a quick fix is all you’re looking for, run this:

curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh

Here’s the code the previous line runs. To sum up, it moves your OS X default postgres binaries into an archive folder and symlinks the homebrew versions in place of them.

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`
do
	if [ -f $LION_POSTGRES_DIR/$i ] 
	then
		sudo mv $LION_POSTGRES_DIR/$i $LION_POSTGRES_DIR/archive/$i
		sudo ln -s $BREW_POSTGRES_DIR/$i $LION_POSTGRES_DIR/$i
	fi
	
	if [ -f $LION_PSQL_DIR/$i ] 
	then
		sudo mv $LION_PSQL_DIR/$i $LION_PSQL_DIR/archive/$i
		sudo ln -s $BREW_POSTGRES_DIR/$i $LION_PSQL_DIR/$i
	fi	
done

If you enjoyed this post, follow me on Twitter

About Paul

PHP, RoR, JS, Obj-C Developer working at @hellofax and @nextmarvel. Father of 2, husband of 1. Follow me at http://twitter.com/pdenya