Setting up PostgreSQL on a Mac (for Django)

Due to my infatuation with the google-app-engine and aep I feel like I haven't set up Django with PostgreSQL in quite a while. In this post, I'll go over the few simple steps you can choose to follow to get postgres up and running on a Mac. If you haven't done so already, please install pip.


Okay, so there's many different ways to install PostgreSQL but I'll go over the point-and-click method a lot of you are used to. First you need to find the thingy to click on so download the PostgreSQL dmg (installation file). At the time of this writing the version is 8.04. After downloading the file, give it a good double-click. A dialog window will pop up enabling you to click on the postgresql-8.4.0-1-osx icon. Give that a good click and follow it up with clicking on "Open" and entering your password.

There's a good probably that you'll be prompted to reboot your computer before the installation continues. If so, please reboot and double click on the dmg after your system starts up again. Following the installation setup, you will be asked to enter the following:

  • Enter a installation directory: /Library/PostgreSQL/8.4 (default)
  • Enter a data directory: /Library/PostgreSQL/8.4/data (default)
  • Enter a superuser password: kryptonite (can be anything)
  • Default port 5432
You may be asked if you'd like StackBuilder to run after the installation completes. Say yes.

StackBuilder will allow you to install a few goodies such as phpPgAdmin to help with DB administration.

Depending on what you clicked on, you may have to go through a few installing procedures. Just click on the next button until all those pop-up windows disappear.


We'll have to add a few items to our environment so open up a terminal window and enter the following:
donnie-demuths-macbook-air$ echo "export PATH=$PATH:/Library/PostgreSQL/8.4/bin" >> .bash_profile
donnie-demuths-macbook-air$ echo "export PGDATA=/Library/PostgreSQL/8.4/data" >> .bash_profile

Those two commands will modify your system's PATH variable and add a new one called PGDATA. After running both, close and reopen a terminal window. You can now use a tool called psql to work with your database in a shell.

Before you use psql, let's start the server via the easy way. Click on your finder and go to Applications > PostgreSQL 8.4 > and double-click on the Start Server button. There you'll also find a Stop Server button when you need it. Obviously, there's way to control your db via the command line but it's pretty easy to forget those commands/scripts.

Once the server is started, open up a terminal and type:
psql -U postgres

To enter the postgres shell with the postgres default user. You'll be prompted to enter in a password, mine was "kryptonite."

If you want to use PostgreSQL with Django, you'll need the psycop2 python package. Install it with the following command:
donnie-demuths-macbook-air$ sudo pip install psycopg2

To install Django you can also use the pip utility:

donnie-demuths-macbook-air$ sudo pip install django

Be sure to add to your bin by typing:

donnie-demuths-macbook-air$ sudo ln -s /Library/Python/2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/bin/ /usr/local/bin/