Skip to main content

How to connecting PostgreSQL with a Django application

django connect with PostgresSQL

In this article, you will learn how to connect to a postgreSQL database to a Python web application framework Django. You will also learn how to edit your Django Project Settings file and connect your web application to a PostgreSQL Database.

After walking through the Django installation process it shows how to get started creating a simple Django application.

Connecting your Django App to a PostgreSQL Database

# Introduction:

Django is a flexible framework for quickly creating Python applications. By default, Django applications are configured to store data into a lightweight SQLite database file. While this works well under some loads, a more traditional DBMS can improve performance in production.

Let's see, How to use a postgres database in Django app :

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 : Next we need to install an adapter for connecting our database with the Django App. We will be using "psycopg2" for this purpose.

  • pip install psycopg2

Step 3 : Now go-to the "settings.py" file of the Django App and Under the settings file for Databases, change the settings from sqlite3 or your current database to the following code.

 ➤ Code: settings.py (sqlite3)

  • DATABASES={

  •   'default':{

  • 'ENGINE': 'django.db.backends.sqlite3',

  • 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

  •   }

  • }

 ➤ Code: settings.py (postgresql)

  1. DATABASES={

  2.   'default':{

  3. 'ENGINE': 'django.db.backends.postgresql_psycopg2',

  4. 'NAME': 'your-db-name',

  5. 'USER': 'user_name',

  6. 'PASSWORD': 'your-db-password',

  7. 'HOST': 'localhost',

  8. 'PORT': '5432',

  9.   }

  10. }

Step 4 : Now run makemigrations command to convert all the models into queries for the database.

  • pip manage.py makemigrations

Step 5 : And finally run migrate command to migrate these changes into our database.

  • pip manage.py migrate

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

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.

Note that: To install psycopg2 adapter, We will also need to install pillow in case we wish to save any pictures in our database. This can be done using. Have fun!

  • pip install pillow

Comments

SMT said…
Great technical articles, thanks for sharing. customer care finder
Unknown said…
This was my clincher in getting through my connection issues. Ty for the article.

Popular Posts

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return

java program for student details using inheritance

# Java program to print student details using "single-level" inheritance In this section, You will learn how to print student details using single inheritance in java, with Scanner and without Scanner class. 1.) With " Scanner " class Let's try to create a simple example :   ➤ Example : student.java; import java.util.Scanner; class StudentDetails {    int roll_no ;    String name , cl ; //creating a function to take student details    void input () { Scanner sc = new Scanner ( System . in );   System . out . print ( "Ente