Sunday, May 27, 2012

Install Postgresql for Django

This should just be referenced for getting postgresql setup for testing purposes.
Install Postgres:
$ sudo apt-get install postgresql
This installs version 9.1 on my system. It automatically creates a user called postgres.
Set the password:
$ sudo passwd postgres
and enter a password twice.
Switch user:
$ su postgres
Create a user named django_login:
$ createuser -P django_login
and enter a password twice. This is the password you will be using in your settings.py file. Enter the Postgres shell:
$ psql template1
Create a database:
CREATE DATABASE django_db OWNER django_login ENCODING 'UTF8';
Get out of the db shell by typing:
$ \q
Edit the Postgres permissions in /etc/postgresql/9.1/main/pg_hba.conf by adding a line as follows after the postgres user line:
local      django_db   django_login   md5
Restart Postgres:
$ sudo /etc/init.d/postgresql restart
Download python-psycopg2 which lets Postgres talk to Python:
$ sudo apt-get install python-psycopg2
Edit your settings.py file in the django app directory:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_db',                      
        'USER': 'django_login',                   
        'PASSWORD': 'your_password',              
        'HOST': 'localhost',                      
        'PORT': '5432',           
    }
}
Make sure that this is the correct port by running:
$ grep postgresql /etc/services
Sync your db while in your django project directory:
$ python manage.py syncdb
If this works, you are now using Postgres with Django!
References: http://blog.iiilx.com/programming/how-to-install-postgres-on-ubuntu-for-django

2 comments: