Install Django, GeoDjango, PostgreSQL & PostGIS on OSX Leopard

June 1, 2009

p230_02

This procedure will loosely follow the directions in the following locations:

I’ll assume you are using the terminal. You are also using OSX 10.5.6 (or later)

1) Install Django

Make a temporary directory to house the install files and enter that directory
mkdir django_install && cd django_install

Download the latest stable version of Django.
wget http://media.djangoproject.com/releases/1.1/Django-1.1-beta-1.tar.gz

Untar it into a temporary directory and cd into the directory
tar zxvf Django-1.1-beta-1.tar.gz && cd Django-1.1-beta-1

run the installer
sudo python setup.py install

These commands will install Django in your Python installation’s site-packages directory.

2) Install GeoDjango prerequisites

This section is from a web browser and the finder. It’s just easier that way.

Download the following drive image files, double click them to mount them as a drive, and run the mpkg installer inside each one. The order is important, so do them one by one:

These all setup your system with the right unix GIS libraries to support GeoDjango

3) Install OSX developer Tools

This bit is a total pain, but you only have to do it once. Break open the CD’s that came with your Mac, and find the one that says XCode or Developer tools or something.

Browse to the installer, and just install it all.

4) Install PostgreSQL & PostGIS

This section is from a web browser and the finder. It’s just easier that way.

Download the following drive image files, double click them to mount them as a drive, and run the mpkg installer inside each one. The order is important, so do them one by one:

5) Install pgAdmin3

This is the MSSQL enterprise manager of postgres. Download it and drag it to your applications folder. It won’t be able to connect to postgres yet, so don’t even both trying.

pgadmin3-1.8.4.dmg

6) Setup some paths

Back to the console now go to your home directory
cd $home

Add postgres binary path to your bash profile
echo "export PATH=$PATH:/usr/local/pgsql/bin" >> .bash_profile

Add a postgres data directory variable to your environment
echo "export PGDATA=/usr/local/pgsql/data" >> .bash_profile

quit the terminal and restart it

6) Install the Python PostgreSQL library

Back into the console now. Install the binding using the python easy_install command
sudo easy_install psycopg2

This may not work…

If for some reason, this install fails (and it failed on one box I installed), you’ll need to download the source, unpack and cd into it, edit setup.cfg with include dir and librarydir set to /usr/local/pgsql/include and /usr/local/pgsql/lib, and then run python setup.py build && python setup.py install

7) Create postgres user

Note, this may not be neccessary for some of you. I’d installed postgres before, and had somehow managed to mess up my box – hence the manual setup of postres user and rights. As Dane mentions in the comments below, this step shouldn’t really be needed. What can I say – I’m a linux hacker at heart, so permissions issues are always surmountable :) . YMMV

Open system preferences > accounts and delete the postgres user.

click the + button and add a new user called postgreSQL, with a short name of “postgres”. Set the password to whatever you’d like. This is not necessary if the user already exists

8 ) Set permissions on postgres data directory

Go to your postgres data directory and sort permissions/ownership. Again, this may not be neccessary for you, but it was for me.

cd /usr/local/pgsql/
chmod -R 0700 data
chown -R postgres data

9) Create scripts to start and stop Postgres

For some reason, it’s not clear how to stop and start postgres from OSX, and using the kyngchaos binaries doesn’t seem to properly start them (or at least it didn’t on my machine). I think this was due to the permissions not being set properly on the data directory.

Go to your home directory
cd $home

create scripts to start/stop postgres && set permissions on them
echo "sudo launchctl load /Library/LaunchDaemons/org.postgresql.postgres.plist" >> start_pg.sh

echo "sudo launchctl unload /Library/LaunchDaemons/org.postgresql.postgres.plist" >> stop_pg.sh
chmod 755 start_pg.sh
chmod 755 stop_pg.sh

Finally, you should be able to start and stop postgres (check the activity monitor for postgres processes, making sure to view “all processes”) by running:

./start_pg.sh

and to stop:
./stop_pg.sh

from your home directory. If you can't see the postgres process in your activity monitor, let me know!)

9) initialise database

Ok, assuming we have postgres up and running and all the bindings sorted, we are ready to initialise our DB for our first project. At this point, I recommend following along from step 2 of the official geoDjango guide.

Make sure you read the code comments for the tutorial - in my case, the postgres shared directory was: /usr/local/pgsql/share, not /usr/share as in the docs.

{ 1 trackback }

Install Django, GeoDjango, PostgreSQL and PostGIS on OSX Leopard | dv8-designs
July 24, 2009 at 9:41 am

{ 11 comments… read them below or add one }

1 Dane Springmeyer June 4, 2009 at 3:07 am

Nice Write-up Simon!

One note on postgresql setup: the ‘postgres’ user is actually not a user but a ‘role’ and normally does not have a home directory or password. ‘postgres’ is designed to initialize and run the database, while administrative privileges are then given to other unix users to be able to connect to certain databases.

I think that creating a unix user named ‘postgres’ is likely what prevented the packaged startup scripts from working. If you delete that user then re-install Postgres Pkg I bet the automatic initialization and startup will be done for you.

Williams got a nice ‘Users and Roles.rtf’ readme inside the PostgreSQL-*.dmg that you can see for all the gory details.

The first bit reads:

“”"
A common confusion for Mac users and/or those new to Postgres is Postgres users and roles.

A user, or as I often call it for clarity (or confusion) a system user, is a user in the operating system. A Mac user. This can be a user you create with System Preferences that can login to the Mac, or a user defined by the system used for some system purpose (ie root, www, …).

A role is a user in the Postgres world. Only Postgres knows about them, and they are used to control who can do what when connecting to Postgres.

The confusion comes mostly because user/role names can overlap, so it’s hard to know where and which passwords are needed. And especially when it comes to the postgres user and role.
“”"

2 Dane Springmeyer June 4, 2009 at 3:12 am

Also!

While its not needed for GeoDjango, since those Frameworks for GDAL, GEOS, SQLite, and others are so handy as dependencies for building other applications (like say, http://www.mapnik.org) I keep an easily copy and paste-able list for my ‘bash_profile’:

Having things like ‘geos-config’ and ‘gdal-config’ on your path will save hours of agony when building things from source:

# put in ~/.bash_profile or /etc/profile
export PATH=/Library/Frameworks/UnixImageIO.framework/Programs:$PATH
export PATH=/Library/Frameworks/PROJ.framework/Programs:$PATH
export PATH=/Library/Frameworks/GEOS.framework/Programs:$PATH
export PATH=/Library/Frameworks/SQLite3.framework/Programs:$PATH
export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
export PATH=/usr/local/pgsql/bin:$PATH

3 simon June 11, 2009 at 1:14 pm

Great tips Dane – thanks! I think yu’re right about my mystery postgres users – they did exist before the install, and probably are what caused the mess up.

4 sydney July 25, 2009 at 3:02 am

Great Tutorial!
THanks for sharing.

5 John Berns July 25, 2009 at 6:35 pm

If you are having problems with step #6:

1) Did you follow the last line of step #5? “quit the terminal and restart it ”

If you did not, the PATH environment variable was not updated and step 6 will fail.

2) If you have restarted the terminal and you get an error like:

“No eggs found in /tmp/easy_install-HM7gFE/psycopg2-2.0.11/egg-dist-tmp-M8Wg2x (setup script problem?)”

You need to update setup tools, Just run:

sudo easy_install -U setuptools

setuptools will update and you should be able to complete step 6. Easy-peasy.

6 John Berns July 26, 2009 at 4:43 am

And Step #9, if you get an error following the instructions for installing geographic_admin such as:

“ImportError: Could not find the GEOS library (tried “geos_c”). Try setting GEOS_LIBRARY_PATH in your settings.”

You will have to add the line

GEOS_LIBRARY_PATH=’/opt/local/lib/libgeos_c.dylib’

or whatever the path is to libgeos_c.dylib on your Mac to settings.py in the geographic_admin project.

7 DBRAND August 8, 2009 at 9:22 am

# put in ~/.bash_profile or /etc/profile
export PATH=/Library/Frameworks/UnixImageIO.framework/Programs:$PATH
export PATH=/Library/Frameworks/PROJ.framework/Programs:$PATH
export PATH=/Library/Frameworks/GEOS.framework/Programs:$PATH
export PATH=/Library/Frameworks/SQLite3.framework/Programs:$PATH
export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
export PATH=/usr/local/pgsql/bin:$PATH ( http://d-brand.net )

8 Jacob Fenwick November 9, 2009 at 4:36 am

I’m having a problem with installing GDAL.

The download link for GDAL didn’t work, so I went to the site where it was located and downloaded GDAL_Framework-1.6.2-4.dmg. When I tried to install GDAL I get the error:

The PROJ framework is required.

I have indeed installed the PROJ framework. I even added DBRAND’s PATH statements if that makes a difference.

9 Jacob Fenwick November 9, 2009 at 5:02 am

Actually, http://www.kyngchaos.com/files/software/unixport/GDAL_Complete.dmg looks like it mitigates the need to install all the frameworks individually.

10 Roberto April 7, 2010 at 3:51 pm

Jacob Fenwick: the new link for GDAL_Complete.dmg is http://www.kyngchaos.com/software/frameworks (there are two versions available 1.6 and 1.7)

11 Roberto April 8, 2010 at 12:58 am

Hello Simon!
Thanks for this tutorial
In step 8 (Create scripts to start and stop postgres) you said: “If you can’t see the postgres process in your activity monitor, let me know!”. Guess what ? yes, I don’t see the postgres process in the activity monitor. For instance, when I execute start_pg.sh the first time there is no message (just the prompt). If I try by a second time the message that shows up is :”org.postgresql.postgres: Already loaded” but nothing in the activity monitor. When trying to connect to server with pgAdmin3, it is not able to do it.
Do you have any ideas please ?
Roberto

Leave a Comment

Previous post: Costing a website or webapp: A guide for the non-developer

Next post: Fine grain caching in rails using Memcache