Posted on

Brew Install Postgresql on OS X Lion

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

  • Michael Nichols

    Thank you so much! I’ve spent hours trying to figure this out. 

  • muchas gracias!

    I just prepended /usr/local/bin to my PATH. I may regret that later. Of course had I known that Lion ships with postgres 9 I may have tried to use that first.

    • You did the right thing here Dan.  There’s no reason to regret this one.  You want to prioritize /usr/local/bin over /usr/bin.

  • Asdfasdf

    On Mac OS X Lion, vi /etc/paths and move /usr/local/bin to the top. Save, exit terminal, type brew doctor and you should see something that says, “raring in brew”. If you run brew doctor before modifying paths, you will see an error from brew.

    • Anonymous

      more practical this.

    • Janko

      This fixes it. Thanks a lot.

    • Anonymous

       Really nice advice!

    • Cypherpunks (a public account)

      Please don’t ever edit /etc/paths; these are defaults used by the system.   You should set your path in $HOME/.bash_profile:

      PATH=/usr/local/bin:$PATH

    • Ian Tinsley

      this fixed it after hours of searching. nice work.

  • Wouiu

    Yes, i have this problem. When i print

    “createdb mydb” i get your error.

    When i try 

    curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh””sh: line 3: brew: command not found”

    What is wrong? I do it on Mac OS X Lion.

    • If you’re getting a brew not found error you haven’t installed homebrew on your system so the problem is something to do with the OS X Lion postgres install.  I wasn’t able to get it working myself so I installed homebrew via: http://mxcl.github.com/homebrew/ and then used it to install postgres.

  • Anonymous

    Thanks, this is exactly what I was looking for!

  • !important — if you already ran bundle install, pg needs to be reinstalled to recognize the correct version:
    gem uninstall pg
    gem install pg
    via: http://tammersaleh.com/posts/installing-postgresql-for-rails-3-1-on-lion

    • Artur

      Save my day. :-) Thanks man

    • panlilu

      Save my day. Thanks! QAQ

  • James

    Awesome, worked great. All I had to do is uninstall and reinstall pg gem.

  • Pedr

    Thanks!

  • Mike

    Thank you very much for posting this script. It ended about an hour of frustration trying to get postgres installed on my iMac.

  • Faizan

    Accidentally ran the script twice and I think it has wiped the system Postgres installation. Does anyone know if I should consider reinstalling Lion? Thanks.

  • Anentropic

    Thank you so much!! Fixed this mystifying problem for me

  • 0101

    Nothing happened when I did this, I just ran brew install postgres (MacBook Air 2011) and ran the 3 commands on the post install info to automatically start the server.

  • David Frey

    This is a lifesaver; however, I discovered I had mismatch problems that surfaced when trying to use pg_dump, so I found the following pattern useful for tracking down binaries manually:

    ls -1 `which pg_dump | xargs dirname`/pg*

    In my case I still had pg_config, pg_dump, pg_dumpall, pg_restore, and pg_upgrade left behind in /usr/bin, probably due to some tinkering before stumbling onto your shell script.

  • Pingback: A Coder’s Life » Blog Archive » Install PostgreSQL on Mac Lion via Homebrew()

  • Thank you!

  • Wez

    Thanks dude. This sorted me out.  

  • hep1989

    hi didn’t work for me..looking for help

  • Gavin

    +1

  • Grayaar

    Worked great! Saved me some headache! ;-)

  • Jon Storer

    Wouldn’t it have been easier to just update the unix $PATH? instead of moving files?

  • Ruaidhrí Devery

    Good man yourself, that fixed it!

  • Tavobarrientos

    this script broke my  default ruby install….

  • Wow Thanks, I was running out of people to ask and way of formulating the problem,
     Wait a sec how do I acquire such skills in the ways of shell scripts? 

    Thank you again

  • Tavo_es111

    why do i get this error???
    fatal: Not a git repository (or any of the parent directories): .gitPassword:

    • mackerman

      I’m getting the same error… Author: Any advice?

  • Tony

    this save me everytime. thanks!

  • Thanks!

  • Soner

    Thank you very much!! I’ve been looking for this solution for 3 hours already!

  • ivik

    Is there a way to reverse your script?
    It has made things worse for me…

  • Jimmy

    Worked like a charm, saved me some trouble, thanks!

  • Henk

    Thank you very much, saved my day.
    One note for other: When using the pg gem, do a gem uninstall pg; gem install pg, after running above script, otherwise the problem persists.

  • Thanks so much for this, I spent an hour searching for a fix on my new mb pro. This took care of it instantly.

    Brad

  • nimo

    Thanks a lot!

  • nick

    awesome

  • Worked for me, thanks a lot :)

  • SM2

    Thanks for the fix!

  • Anonymous

    + 1 Thanks

  • jergo_mayor

    Hello, sorry to make this question, but i jus have to run “curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh” and all gets fixed? or do i need to do something else?

    i ran the curl command and the problem is still there. what i’m wrong?

    thanks.

  • tbrooks007

    WTF thanks a lot this was a big help! I ran brew doctor and just fixed by appending to my path in my bash_profile. This helped me figure out what the ultimate problem was.

  • Jessica Thrasher

    Thanks!

  • I encountered this error while running a syncdb for my Django/Python app. I am running PostgreSQL 9.3.5 on my Mac OS 10.7.5. The solution for my case was different. I just had to set the HOST attribute of the DATABASES setting in the settings.py to ‘localhost’.

    ‘HOST’: ‘localhost’,

    And it solved the problem.

  • Thanks for this!