How to Connect PostgreSQL Database in Django Project

How to use PostgreSQL with Django | EDB
Django and PostgreSQL

Learn how to connect a PostgreSQL database with Django App. Django is the most popular web development python framework. It is an open-source web framework and since it is written in Python programming language it is backed by a strong community of programmers. It allows for scalability, re-usability, and rapid development.

PostgreSQL is a powerful, open source object-relational database system with over 30 years of active development, it has a wide range of uses and applications throughout the field of technology. It has earned a strong reputation for its reliability, features, robustness, and fast performance.

In this tutorial, you will learn how to connect to a postgreSQL database to a django app. You’ll learn how to create your Django project in Virtual Environment. You will also learn how to edit your Django Project Settings file and connect your web application to a PostgreSQL Database.

Prerequisites – A system loaded with Windows, Ubuntu or Mac OS and a Django App whose database you wish to migrate to PostgreSQL should be loaded on your system before proceeding through this tutorial.

Connecting your Django App to a PostgreSQL Database

pgAdmin Panel

Step 1. When you open the pgadmin panel you will view a server icon in the navigation at the left side of the window. Click on it to open the databases icon. Right Click on the databases icon to create a new database and fill the prerequisites for creating the database.

Step 2. Now go to the settings.py file of the Django App. Under the settings for Databases, change the settings from sqlite3 or your current database to the following.

DATABASES={
   'default':{
      'ENGINE':'django.db.backends.sqlite3',
      'NAME':os.path.join(BASE_DIR,'db.sqlite3'),
   }
}
DATABASES={
   'default':{
      'ENGINE':'django.db.backends.postgresql_psycopg2',
      'NAME':'project_name',
      'USER':'user_name',
      'PASSWORD':'password',
      'HOST':'localhost',
      'PORT':'',
   }
}

Step 3. Next we need to install an adapter for connecting our database with the Django App. We will be using psycopg2 for this purpose.

psycopg2

Use the command.

pip install psycopg2

To install this adapter. We will also need to install pillow in case we wish to save any pictures in our database. This can be done using.

pip install pillow

Step 4. Now run 

python manage.py makemigrations

To run convert all the models into queries for the database.

Step 5. Now run 

python manage.py migrate

To migrate these changes into our database.

You can go to the pgAdmin panel to check the changes on the database that you have just made in the form of tables that are visible in the following column.

Servers>PostgreSQL>Databases>DB_Name>Schema>Public>Tables

Conclusion

We have created a database and applied the configuration to our projects. Now you can create your own models and apply migrations accordingly.

The Tutorial above explains the method/steps to connect a postgreSQL database to Django app using psycopg2 adapter. This will allow us to boost the performance and security of our development.

Leave a Reply

Your email address will not be published. Required fields are marked *