In the debate between databases, the Django community has time and time again shown a preference for Postgres. I was lucky enough to have started with Postgres from the beginning (with the brief exception of sqlite for the tutorial). However sometimes I still run into problems when I have to set up a new server and forget how to configure Postgres, mainly with respect to user authentication. So I've come up with some concise instructions for getting a clean Ubuntu install running with Postgres 9.1, Django 1.5, and psycopg2.
Packages
- Postgres The database! Pretty obvious that this is needed!
- postgresql-server-dev-9.1 A debian package needed to compile the python database adapter (psycopg2)
- python-dev Python header files which are also needed to compile psycopg2
- python-pip A Python package installer, works well with virtual environments and version pinning
- psycopg2 A Postgres database adapter for Python
- Django Our favorite Python web framework!
Installation Command
sudo apt-get install postgresql postgresql-server-dev-9.1 python-dev python-pip
sudo pip install Django psycopg2
Note that the postgresql-server-dev package has a version on it, this should match your postgresql version
Configuring Postgres
Now create a user to access the database with and create an empty database for Django to use later.
First, access postgres as the 'postgres' user via the command line utility.
sudo -u postgres psql
Then, create a user for Django, create a new database, and give the new user access:
postgres=# CREATE USER django WITH PASSWORD 'djangopass';
CREATE ROLE
postgres=# CREATE DATABASE pg_test;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE pg_test to django;
GRANT
postgres=# \q
Finally, configure Postgres to allow socket connections with md5 (Django can use sockets or tcp). To do this, edit the pg_hba.conf file which, on Ubuntu, should be at /etc/postgresql/9.1/main/pg_hba.conf
. Near the bottom, change the lines so that "local" connections use the md5 method.
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Note that these settings are fairly loose in terms of access, but they are all local only
Restart Postgres to apply the changes.
sudo service postgresql restart
Setup Django
Create a new Django project and edit the database settings.
django-admin.py startproject pg_test
cd pg_test
vi pg_test/settings.py
Change the database settings to access the new postgres database with the new user.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2'
'NAME': 'pg_test',
'USER': 'django',
'PASSWORD': 'djangopass',
'HOST': '', # Leave blank for socket connection
'PORT': '', # default postgres port is 5432 for the curious
}
}
Check if everything works
python manage.py syncdb
If syncdb completes without error, Postgres has been installed correctly!