25 November 2010

Install PostgreSQL 9 on OS X


You're not still using MacPorts, are you? Please go get Homebrew immediately, the new-hotness package manager for OS X written in Ruby. It amazing how simple Homebrew makes this process in comparison to my old instructions on this topic. It's also nice to be able to peer into friendly-Ruby Homebrew Formulae to see the exact build commands.

Uninstall Old Versions

Initially, I tried a brew install postgresql over the top of the existing version that was installed via non-Homebrew means. When it was complete, psql --version was still showing 8.4.x. I found it is best to uninstall any old PostgreSQL versions first. Of course, back up your data with pgAdmin or pg_dump.

First, uninstall any Homebrew versions. The --force option makes it uninstall all versions.

brew rm postgresql --force

Via Uninstaller

Second, uninstall any versions installed by other means.

If you installed using EnterpriseDB, you likely have an uninstall-postgresql.app file in your install directory. Double-click that to uninstall their Postgres install.


I went the manual route. Change paths accordingly for your version.

sudo /sbin/SystemStarter stop postgresql-8.4
sudo rm -rf /Applications/PostgreSQL\ 8.4
sudo rm -rf /etc/postgres-reg.ini
sudo rm -rf /Library/StartupItems/postgresql-8.4
sudo rm -rf /Library/PostgreSQL/8.4
sudo dscl . delete /users/postgres

Edit /etc/profile and delete any lines that reference "postgres". For me, there was just one line that had to do with loading Postgres' Environment file.

nano /etc/profile

I rebooted here, to make sure the Postgres startup items were truly gone. Open "Activity Monitor" and ensure there are no "postgres" processes.

Install PostgreSQL

brew update
brew install postgresql

Follow the instructions at the end of the install to initialize the DB, add startup items, and start Postgres. For me, it was:

initdb /usr/local/var/postgres
cp /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Important Note About Authentication

The Homebrew formula does not create a "postgres" user, as was common up to this point. Out of the box, you authenticate against the DB using the same user account under which you ran brew install. It's perfectly acceptable to leave it that way on your Dev machine. Don't forget to change that if this is a Production environment.


Install Instrumentation so pgAdmin doesn't yell at you. Run this as the same user under which you installed Postgres.

psql postgres -c 'CREATE EXTENSION "adminpack";'


Launch pgAdmin, add a DB for "localhost", and use your username and password.

Install the PG Gem

This installs the "pg" gem that Matz himself wrote. It is recommended over the old "postgres" gem. I do not believe you need to give it Architecture Flags anymore (env ARCHFLAGS="-arch x86_64" gem install pg), so just do:

gem install pg

Filed under Mac