Fixing Postgres connection errors on Mountain Lion

November
27
2012

RFZ2 Survivor badge from Code SchoolI’ve recently started playing around with Ruby on Rails, starting with the excellent Rails for Zombies Redux and Rails for Zombies 2 courses from Code School (highly recommended). I made it all the way through both over the course of last weekend (as anyone following me on Twitter feed can attest), finally earning the much coveted RFZ2 Survivor badge seen at right.

Wanting to keep plugging away at it, I decided to watch the also excellent Rails App From Scratch webcast on Code School TV. I made it all of about three minutes in before they recommended installing Postgres, so I dutifully paused, jumped into Terminal, and fired up homebrew. And that’s when Mountain Lion reared its ugly head.

Seems, for some reason, that Mountain Lion doesn’t create a socket file in the place that rake is expecting to find it. After getting everything installed and my user and databases created, I was able to connect fine from the psql tool and see that which psql returned the correct one in /usr/local/bin/psql. Running rake db:create or rake db:migrate would throw a nice long stack trace starting with:

could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

I tried a number of the suggested fixes in various StackOverflow posts with no luck. It was Ben’s answer here that finally tipped me off. If you’re having the same problem and can’t resolve it, try:

mkdir /var/pgsql_socket/ 
ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/ 

(Create the directory then make a symbolic link to where the socket file actually lives).

Hope that helps!

9 comments
fermtzdiaz
fermtzdiaz

Super, tks.

i try this formula

sudo mkdir /var/pgsql_socket/ 

sudo chown postgres:postgres /var/pgsql_socket/ 

sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

kgautreaux
kgautreaux

Just fwiw, I actually had to create the opposite link.  /var/pgsql_socket/.s.PGSQL.5432 did exist on my machine but was not symlinked to /private/tmp/.s.PGSQL.5432 for some reason.

Euan
Euan

Thanks Jay. This sorted me out! But now Induction is doing something really weird. Now when I fill in my details, Induction seems to be taking the username field in the connect form and using that to find the postgres database and I get this error: FATAL: database "myusername" does not exist Have you seen this one?

Chim Rijolds
Chim Rijolds

always with the symbolic links thank you Jay Goldman you stallion of a man

CDN
CDN

Thanks, Jay. This solved my issue as well (Mac OS X 10.8.3) after I hosed my local brew install of postgresql.

Steven Vance
Steven Vance

This solved my issue. I saw an answer on StackExchange that seemed like it would help me, but just creating that folder didn't. I had to create that symbolic link.

jaygoldman
jaygoldman moderator

@Euan I've never actually used Induction so I'm not sure. Can you connect to Postgres from the command line okay?

Trackbacks

  1. [...] Fixing Postgres connection errors on Mountain Lionを参考に、シンボリックリンクをはった。 [...]

  2. [...] Postgres connection error on OS X Mountain Lion http://jaygoldman.com/2012/11/fixing-postgres-connection-errors-on-mountain-lion/ (funny because with Node there were no [...]

  3. […] the database. The fix that worked for us (and for my first attempt several years ago,) comes from Jay Goldman’s post here (which is itself based on an answer from Ben on […]