Installing Django with PostgreSQL

We are going to Install Django with PostgreSQL as the backend database, and create our first project.

  • Installing Django

Install Django and required libraries under your Python virtual environment.

$ pip install django psycopg2

Psycopg is a PostgreSQL adapter for Python, needed for Django to be able to connect to PostgeSQL.

  • Checking Django installation
$ python -m django –version
1.10.3
  • Preparing PostgreSQL

Create a user and a database for your project on PostgreSQL.

$ createuser -P django-user
$ created django-project -e -O django-user -E UTF-8
  • Creating a project
$ django-admin start project django_project
  • Setting PostgreSQL on Django

 By default, the newly generated Django project will use SQLite as the backed database, we are going to change the database settings to use postgresql_psycopg2 engine (adapter) to connect to PostgreSQL.

$ vi django_base/settings.py

In settings.py in the DATABASE section change it to:

$ DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'django-project',
        'USER': 'django-user',
        'PASSWORD': 'your password',
        'HOST': 'localist',
        'PORT': '',
    }
}
  • Migrating the database
$ python manage.py make migrations && python manage.py migrate
No changes detected
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
  • Testing the project
$ cd django_base/
$ python manage.py run server
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

November 23, 2016 - 18:45:44
Django version 1.10.3, using settings 'django_base.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open your web browser at http://127.0.0.1:8000/

If everything went fine you’ll see a message like this:

It worked!
Congratulations on your first Django-powered page.

Notes:

Backend engine for PostgreSQL is named django.db.backends.postgresql since Django 1.9 in older releases is django.db.backends.postgresql_psycopg2.

References:

https://www.djangoproject.com/start/
https://docs.djangoproject.com/en/1.10/intro/tutorial01/
https://docs.djangoproject.com/en/1.10/ref/settings/#databases
https://docs.djangoproject.com/en/1.10/ref/settings/#engine
https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04
http://initd.org/psycopg/
https://wiki.postgresql.org/wiki/Psycopg2_Tutorial