Last Updated: February 25, 2016
· aderyabin

Install Postgresql on Mountain Lion

brew install postgresql
initdb /usr/local/var/postgres -E utf8
mkdir -p ~/Library/LaunchAgents 
cp /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/ 
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Then manually start the database with:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Next, need to fix Mountain Lion’s genius behaviour to have postgres open a socket in /var/pgsqlsocketalt, which nobody looks at.

In terms of terminal commands:
mkdir /var/pgsql_socket sudo chown $USER /var/pgsql_socket
Open /usr/local/var/postgres/postgresql.conf in a text editor, and uncomment + edit the unixsocketdirectory key to:
unix_socket_directory = '/var/pgsql_socket'

Based on Setting Up a Development Environment on Mountain Lion

7 Responses
Add your response is the easiest way to run PostgreSQL on the Mac

over 1 year ago ·

Thank you for this, no one mentions that Mountain Lion opens the socket at /var/pgsqlsocketalt

over 1 year ago ·

With you have to be sure that which psql points to /Applications/

over 1 year ago · for easiest way to start or stop postgres

over 1 year ago ·

Saved me a lot of headache. Thanks!

over 1 year ago ·

I too was having trouble connecting to Postgres after upgrading to 9.2 on Mountain Lion, but it all turned out to be do with my PATH variable.

You need to make sure that /usr/local/bin preceeds /usr/bin in your PATH on ML, as by default it doesn't (Mac's path_helper is run from /etc/profile and will put /usr/bin first). ML ships with Postgres client binaries, which can cause confusion when starting Postgres, initialising the db, and building client libraries (e.g. the pg gem).

I was wise to this so rebuilt the pg gem, but failed to do it in a shell that had /usr/bin before /usr/local/bin.

Uninstalling pg and then reinstalling it having set PATH up to use /usr/local solved all my issues, and I didn't need to create the old /var/pgsql_socket directory.

over 1 year ago ·

Thanks for comment. I switched to vagrant+chef to isolate my development environment from my Mac.

over 1 year ago ·