Wednesday, September 25, 2013

Postgres SQL connection error - Is the server running locally...

I ran into an issue on my Mac Mini today while setting up for a development footprint for a Rails 4 project I'm starting to work on this week.   This new project requires Postgres which I have not used before.  I ran into a snag installing it on my Mac Mini which is running OS X version 10.8.5

I proceeded to use homebrew to install by preforming these steps:

brew update
brew install postgres

In typical fashion the recipe presented some notes about continuing the setup.  The next steps were:

initdb /usr/local/var/postgres -E utf8
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Then I needed to use the postgres client to create a user and new database.  When I issued the command

psql -d template1

I received the following error:

psql: 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"?

The initial indication here is that postgres wasn't running.  This post suggests verifying that by using the command:

ps auxw | grep post

and confirming that postgres was actually running.  In my case it definitely was running.  Several processes were matched.

Then I stumbled across numerous post suggesting that the PATH was set wrong.  That possibly Mac OS X Mountain Lion actually has pre-installed a postgres client that looks in a different place for the connection socket.  I don't know if that's true or not.  However,  unfortunately for me, it turns out that I was actually running an older / different version of the client located in /usr/bin  instead of the newly installed on from homebrew located in /usr/local/bin

You can check by the command:

which psql

and for me that result was /usr/bin

In my case the /usr/bin directory is in my PATH prior to the /usr/local/bin so rather than reverse that (like I probably should) I opted not to have any other surprises by a PATH change and simply copied the new psql client from /usr/local/bin to /usr/local

Hopefully this helps someone recover from this issue quickly.