Full guide to build Django 1.3.1, Apache2 + ModWSGI and PostgreSQL in Ubuntu 11.10 Oneiric. There are several dependencies packages, python modules and a little configuration to make Django run with PostgreSQL. Let start!
1. Install dependencies Ubuntu packages
We need to install base packages, python modules, postgresql , apache2 and mod-wsgi.
1 2 3 4 5 | sudo apt-get update && sudo apt-get upgrade sudo apt-get install apache2 libapache2-mod-wsgi sudo apt-get install python-setuptools python-pip build-essential nmap python-dev sudo apt-get install postgresql libpq-dev pgadmin3 sudo pip install psycopg2==2.4.1 |
2.Download and install Django 1.3.1
Go to https://www.djangoproject.com/download/ and Download latest Django version. Then extract it and install with :
1 | sudo python setup.py install |
Now you have Django installed in your Ubuntu 11.10 Oneiric.
3. Configure PostgreSQL
If you have MySQL based, actually PostgreSQL isn’t too different with it. First, you must configure password for default account (postgres) and you can create account later. Also, you can create database and assign user permission on it.
We will make our PostgreSQL can be accessed from remote. First, “sudo vim /etc/postgresql/9.1/main/postgresql.conf” and set this :
1 | listen_addresses = ‘*’ |
Add permission to another host to access PostgreSQL remotely by editing “sudo vim /etc/postgresql/9.1/main/pg_hba.conf”. For instance, I want allowed IP range 33.33.33.1-30 ( I use Vagrant ). So I should add it :
1 | host all all 33.33.33.1/30 md5 |
To reset default password for account “postgres” :
1 2 3 | sudo su postgres -c psql postgres ALTER USER postgres WITH PASSWORD ‘<your-new-password-here>’; q |
To create new user in PostgreSQL (for instance, django_login) :
1 | sudo -u postgres createuser -P django_login |
To create new database in PostgreSQL :
1 2 | sudo -u postgres psql CREATE DATABASE <your-django-database> OWNER <your-username-here> ENCODING ‘UTF8’; |
To see PostgreSQL cheatsheet : http://www.postgresonline.com/special_feature.php?sf_name=postgresql83_psql_cheatsheet&outputformat=html
Don’t forget to restart postgresql service to use new configuration by :
1 | sudo service postgresql restart |
4. Configure settings.py
1 2 3 4 5 6 7 8 9 10 | DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’, # Add ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’. ‘NAME’: ‘<your-database-here>’, # Or path to database file if using sqlite3. ‘USER’: ‘<your-username-here>’, # Not used with sqlite3. ‘PASSWORD’: ‘<your-password-here>’, # Not used with sqlite3. ‘HOST’: ‘localhost’, # Set to empty string for localhost. Not used with sqlite3. ‘PORT’: ‘5432’, # Set to empty string for default. Not used with sqlite3. } } |
5. Export table postgreSQL
1 | pg_dump <DATABASE> -t <TABLE> -f <FILE> -U postgres -W |
6. Import table postgreSQL
1 | psql <DATABASE> < <FILE> -U postgres -W |
Now all it’s done. You should do “python manage.py syncdb” before running server with “python manage.py runserver”. Cheers! 🙂