Tuesday, October 18, 2011

PostgreSQL and Django in Ubuntu Natty

I wanted to create to test my Django 1.3 application using PostgreSQL. I'd previously being using SQLite.

I used a very simple, but not very secure setup—making my user account into a database superuser and then using ident authentication to control access. The goal is minimal hassle, not security.

I'm running Ubuntu Natty which comes with PostgreSQL 8.4.

  1. Install PostgreSQL and its Python adapter.
    sudo apt-get install postgresql python-psycopg2
    
  2. Make your development user into a PostgreSQL superuser. This will allow you (and Django) to create databases. Substitute your username for <user>.
    sudo -u postgres createuser --superuser <user>
    
  3. Create a database for your Django project. Substitute your Django database name for <database>.
    /usr/lib/postgresql/8.4/bin/createdb <database>
    
  4. Edit your Django settings.py file to use your new PostgreSQL database and to use ident authentication as your current user.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': '<database>',
            'USER': '<user>'
        }
    }
    
  5. Test your connection. Substitute <projectname> for your Django project name.
    python <projectname>/manage.py sql
    

No comments:

Post a Comment