Configure Django with postgres

Django comes preconfigured with SQLite db. This is goood for playing with django features or doing some lightweight apps. For big applications we have to use the mysql or postgreSQL db.

This article will give a step by step procedure to install and configure postgreSQL with django framework.

Step 1 – Create python virtualenv for our demo app. You can check this article for installing and using virtualenvwrapper.
Step 2– Once virtualenv is created, we will install django into it.

pip install django

Step 3 – Create demo project using django-admin

django-admin startproject django_pgdemo

Step 4 – Install postgres : If postgresql is not installed install it by running below commands.

sudo apt-get update (do this if not done already in this session)
sudo apt-get install postgresql postgresql-contrib

Step 5 – Install psycopg (python adapter for postgreSQL)

pip install psycopg2

Step 6 – Create postgres DB

sudo -u postgres createuser mangesh

Note – I am creating user with same username as ubuntu user so that peer authentication will work and I dont need to provide login credetial for local postgresql db.

Step 7 – Create DB and make previously created user as owner of DB

sudo -u postgres createdb django_pgdemo -O mangesh

Step 8 – Configure DB in Django settings file. The peer authentication method works by obtaining the client’s operating system user name. SO I dont need to provide username/password here. This is just for demo purpose. For production environment we have to provide all the details like Host, Username and Password.

DATABASES = {
 'default': {
 'ENGINE': 'django.db.backends.postgresql_psycopg2',
 'NAME': 'django_pgdemo',
 }
}

Here is varient with all the options configured –

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_pgdemo', # db name
        'USER': 'mangesh', # db username
        'PASSWORD': '123', # password for user
        'HOST': 'localhost',
        'PORT': '5432', # Port for postgresql
    }
}

Step 9 – Run django migrations

python manage.py migrate

– Create django admin site superuser.  Create superuser command will prompt username, password and email on console.

python manage.py createsuperuser

Step 10 – Once the superuser is created, run development server

python manage.py runserver

Step 11 – Django admin site is up and running

Navigate to http://127.0.0.1:8000/admin/ You should see django admin site login page. Login with superuser created in earlier step.

Leave comments if you have any issues or doubts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s