OSX Lion ships with the PostgreSQL client tools, as well as a _postgres daemon user. I wanted to add a full PostgreSQL server in a way that was compatible with these tools and without creating an extra user account.

This recipe uses HomeBrew to do the heavy lifting. If you haven't already got it, head over to http://mxcl.github.com/homebrew and install it.

First, make sure brew is up to date:

$ brew update

Start the install process:

$ brew install postgresql

This will take a few minutes to download and eventually end with something like this:

==> Summary
/usr/local/Cellar/postgresql/9.0.4: 2577 files, 34M, built in 77 seconds

Next, initialize the database storage with this command:

$ initdb /usr/local/var/postgres

The built-in psql command (and many apps) expect to connect to a local PostgreSQL database using a unix domain socket in /var/pgsql_socket. This is currently owned by the _postgres user so we need to fix that:

$ sudo chown $USER /var/pgsql_socket/

Now create a launchctl configuration to start the database automatically. First, copy the default config file:

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/

Now, edit ~/Library/LaunchAgents/org.postgresql.postgres.plist in a text editor and look for this line:


Paste in the following lines immediately below it:


Start the database server like this:

$ launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist

Add the _postgres account as a database user (with no password):

$ createuser -a -d _postgres

Finally, verify that it's all working by connecting:

$ psql template1 -U _postgres
psql (9.0.4)
Type "help" for help.


You should also be able to connect with a TCP/IP connection to localhost, port 5432.

Published on Aug 9, 2011

© Copyright 2009-2014 Peer Assembly Ltd.