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
  • 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/

In in the DATABASE section change it to:

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'django-project',
        'USER': 'django-user',
        'PASSWORD': 'your password',
        'HOST': 'localist',
        'PORT': '',
  • Migrating the database
$ python make migrations && python 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 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 migrate' to apply them.

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

Open your web browser at

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

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


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