First and foremost, I highly recommend that you download VirtualBox and Vagrant to bootstrap a development server on your Mac.

That being said there are times when you’re doing a simple test or tutorial and you’d like to be able to run PostgreSQL on your local machine.

I’m writing this as I found setting this up on OS X 10.10 Yosemite to be a bit of a pain, since a simple homebrew install didn’t do the trick for me.

Caution: Before going any further, this tutorial assumes you do not have a prior installation that has data you actually want to keep. If this is not the case then stop now, and backup your data to restore later.

Fire Up That Command Line, It’s Time to Dive In.

This is not a production environment and chances are this is a Mac that you alone use. Therefore it’s perfectly acceptable to optimize for simplicity and ease of use.

If you’ve never installed PostgreSQL in the past (if you have skip ahead one step), then create the data directory you need and set the permissions so your user account has full access.

mkdir /usr/local/var/postgres
chmod 770 /usr/local/var/postgres

If you’ve installed PostgreSQL before (if you haven’t you should follow the first step and skip this one) and do not have data you care about, follow these steps to setup your data directory with full access for your user account.
If the first command produces an error just ignore it, you’re making sure PostgreSQL isn’t loaded already and trying to run before you have things setup correctly.

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
sudo rm -rf /usr/local/var/postgres
mkdir /usr/local/var/postgres
chmod 770 /usr/local/var/postgres

Now install PostgreSQL using [homebrew][]. If you already have it or an old build just uninstall that first. (hint: brew uninstall postgresql)

brew install postgresql

Next initialize the data directory.

initdb -D /usr/local/var/postgres

After the initialization is complete, setup PostgreSQL to run on boot with these two commands. The first creates a symlink to a launch agent and the second tells launchctl that that agent would be loaded now and on boot.

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

PostgreSQL will now start up and be ready to use. One trick is to run the createdb command with no arguments to create a database named after your user account. You never have to do anything with this database, but this allows you to run the psql command unadorned to connect to PostgreSQL.

TL;DR
Do this now and save keystrokes later.

createdb

Now you can connect to your locally running PostgreSQL server just by running this short command.

psql

You’re now ready to create a database or two for your projects to use and start hacking. Have fun! :-)