Wednesday, March 21, 2012

Installing PostgreSQL 9.1.3 on Mac OS X Lion

Unsatisfied and undaunted by the foreboding discussion on installation troubles ("PostgreSQL 9.1 Installer Fails on OS X Lion"), I decided to follow the official PostgreSQL instructions to install the software from source. All so that I could build Ruby on Rails apps to be deployed to Heroku.

Note: PostgreSQL 9.0.5 appeared to have been bundled with my Lion installation, as seen with pg_config before installing 9.1.3. But I wasn't sure how well it worked since Apple provides zero documentation on this bundled installation, and initdb was not located in a known path.

So, in short, here are the steps I followed to install PostgreSQL 9.1.3 on Mac OS X Lion 10.7.3 from the source code.

# Make sure you have the latest version of
# GNU Make for Mac OS X. This can be downloaded
# through Xcode 4.3 by installing the Command
# Line Tools.

# Download the source code from the PostgreSQL
# website, and start this procedure in the
# expanded directory containing the source files.

./configure
make
sudo make install

# At this point, assuming installation was
# successful, create a new user to serve as the
# unprivileged user that will own the server
# process.

# Open System Preferences to create a new user.
# New Account:  Standard
# Full Name:    PostgreSQL Agent
# Account name: postgres

cd /usr/local/pgsql/
sudo mkdir data
sudo chown postgres data
sudo mkdir log
sudo chown postgres log

# At this point, we're done with configuration
# and ready to start the server process.

sudo su - postgres

# The following commands will be run as the
# PostgreSQL Agent user.

cd /usr/local/pgsql/
bin/initdb -D data/
bin/postgres -D data/ >log/logfile 2>&1 &

# To verify that the server is working properly,
# let's create a test database and see whether
# we can connect using the interactive terminal.

bin/createdb test
bin/psql test

If all went well, you should see something like the screenshot below.


Finally, we can move on to the fun stuff!