Meditations on programming, startups, and technology
New Relic

Installing Django with PostgreSQL on Ubuntu

This how-to is essentially the same as my previous one, only this time I’ve provided step-by-step instructions for installing Django with PostgreSQL on Ubuntu 7.10.

First and foremost, we are going to install Django from its svn repository, as opposed to obtaining the 0.96 release archive. The reason for this is that the trunk version implements a few new features. The development code is also rather stable and used by most people in production mode, even for sites like the Washington Post.

Install Subversion

sudo apt-get install subversion

Checkout Django

svn co http://code.djangoproject.com/svn/django/trunk django_trunk

Tell Python where Django is

Ubuntu already ships with Python 2.5.1, thus you won’t have to install it. You can verify this by running python in your shell (use exit() to get out of the python shell). What you need to do is inform Python about the location of your django_trunk directory. To do this create the following file:

/usr/lib/python2.5/site-packages/django.pth

Within this file, place only one line containing the path to your django_trunk folder. In my case, this is:

/home/antonio/django_trunk

Of course, change it to the full path location of the directory on your filesystem.

Add django-admin.py to your PATH

The bin directory within the django folder (which is inside django_trunk itself) contains several management utilities. We need therefore to add the following to the PATH (again, change it to your own location):

/home/antonio/django_trunk/django/bin

How you go about doing this, depends on the shell you are using, and I’m assuming you are able to export a shell variable on your own. In case you are using the bash shell (as I do) you could export it in .bashrc. Alternatively, you could just create a symlink to the utility django-admin.py in /usr/bin, but I recommend the former approach.

Install PostgreSQL and Psycopg2

sudo apt-get install postgresql pgadmin3 python-psycopg2

This will install PostgreSQL 8.2.5, PgAdmin III and the driver Psycopg2 for you. Most people at this point will ask, what’s the default password for PostgreSQL on Ubuntu? You can use the following instructions to set the password for the user postgres both in Ubuntu and within PostgreSQL:

sudo su -
passwd postgres
su postgres
psql template1

The last instruction should open the psql shell, where you can run the following:

ALTER USER postgres WITH ENCRYPTED PASSWORD 'mypassword';

Verify the installation

You should be all set now, but let’s verify this right away. Open the shell and run the following instructions inside the python shell (start off with the python command).

>>> import django
>>> print django.VERSION
(0, 97, 'pre')
>>> import psycopg2
>>> psycopg2.apilevel
'2.0'

By running exit() get out of the python shell, and verify that django-admin.py is in your path:

django-admin.py
Type 'django-admin.py help' for usage.

If you obtain a similar output for all three of them, you are really set.

Where to go from here

Now that Django is installed, you can go read the Django Book 1.0 that’s available for free online. Something equally well done and useful is really missing from the Rails community. Above all, experiment, Django (and programming in general) is learnt by doing. The Definitive Guide to Django: Web Development Done Right is also available for purchase in its deadtree version, which just came out. It’s cheap and it’s already a best seller on Amazon. Despite the availably of a free version online, I like having paper versions of tech books so that I can read without staring at the monitor. Furthermore, I feel like rewarding the authors (who are also the framework creators), while encouraging publishing companies that are willing to allow authors to make their books available for free on the web. Well done guys!


If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

receive my posts by email

7 Responses to “Installing Django with PostgreSQL on Ubuntu”

  1. John A says:

    Thanks – Being new to Linux this helped me get up-and-running with Ubuntu.

    A couple points:
    – This worked great with Ubuntu Desktop 7.10, but not quite with Ubuntu Server 7.10. I couldn’t apt-get pgadmin3 (assuming because it’s a GUI).

    – One of your paths referred to ~/django_trunk while another pointed to ~/djtrunk. No big deal – just thought I’d let you know.

    Nice job and thanks again!

  2. Hi John A,

    thanks for spotting the djtrunk, I have fixed it now. :)
    pgadmin3 is not essential, just very useful on desktop systems. And yes, your problems were probably due to the fact that it’s a GUI application.

  3. Jean-Henri says:

    Great ! I already lost plenty of time setting this up. You made me win a couple of hours, thanks a lot !

  4. Mihai says:

    Thanks a lot.Setting up Django(0.96) on Ubuntu 8.04 is now even easier: no further paths or symlink to mess up with.

  5. Good article, as many developers are used to MySQL is always good showing them how to setup PostgreSQL.

    Now, if you want to go further and setup your production environment this tutorial on setting up Django with Lighty and Apache for Ubuntu may help you.

    Cheers.

  6. Wim says:

    Antonio,

    Thank you for your great installation guide. Everything worked like a charm.

    With kind regards

  7. Brock Pagnello says:

    Great article. Really simplified the process of installing Django/PostgreSQL. Thanks a lot! Most other articles are for MySQL.

    I installed the latest Ubuntu – 9.10, which comes with python2.6; and Django 1.2 (beta). Here are some updated notes for the latest versions:
    -in python2.6, they use a dist-packages folder instead of site-packages folder:
    /usr/lib/python2.6/dist-packages/django.pth
    -The django.VERSION command will now print something like this:
    (1, 2, 0, ‘beta’, 1)
    – The easiest way for people to append to their path is to just do it in terminal with this command:
    PATH=$PATH:/home/antonio/django_trunk/django/bin

    cheers

Leave a Reply

I sincerely welcome and appreciate your comments, whether in agreement or dissenting with my article. However, trolling will not be tolerated. Comments are automatically closed 15 days after the publication of each article.

Copyright © 2005-2014 Antonio Cangiano. All rights reserved.