Skip to content
    geeksforgeeks
    • Interview Prep
      • DSA
      • Interview Corner
      • Aptitude & Reasoning
      • Practice Coding Problems
      • All Courses
    • Tutorials
      • Python
      • Java
      • ML & Data Science
      • Programming Languages
      • Web Development
      • CS Subjects
      • DevOps
      • Software and Tools
      • School Learning
    • Tracks
      • Languages
        • Python
        • C
        • C++
        • Java
        • Advanced Java
        • SQL
        • JavaScript
        • C#
      • Interview Preparation
        • GfG 160
        • GfG 360
        • System Design
        • Core Subjects
        • Interview Questions
        • Interview Puzzles
        • Aptitude and Reasoning
        • Product Management
        • Computer Organisation and Architecture
      • Data Science
        • Python
        • Data Analytics
        • Complete Data Science
        • Gen AI
        • Agentic AI
      • Dev Skills
        • Full-Stack Web Dev
        • DevOps
        • Software Testing
        • CyberSecurity
        • NextJS
        • Git
      • Tools
        • Computer Fundamentals
        • AI Tools
        • MS Excel & Google Sheets
        • MS Word & Google Docs
      • Maths
        • Maths For Computer Science
        • Engineering Mathematics
        • School Maths
    • DSA
    • Practice Problems
    • C
    • C++
    • Java
    • Python
    • JavaScript
    • Data Science
    • Machine Learning
    • Courses
    • Linux
    • DevOps
    • SQL
    • Web Development
    • System Design
    • Aptitude
    Open In App

    How to use PostgreSQL Database in Django?

    Last Updated : 23 Jul, 2025
    Comments
    Improve
    Suggest changes
    11 Likes
    Like
    Report
    See More

    This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are  some major differences that you should be consider when you are choosing a database for your applications.

    Also checkout - Difference between SQLite and PostgreSQL

    Setting up PostgreSQL in Django 

    First create a virtual env so to do that first install virtualenv using this command

    pip install virtualenv

    then we will create a virtualenv named gfg using 

    virtualenv gfg

    to enter in the virtual environment create use 

    now we will install Django here so I am using Django 2.2 

    pip install django==2.2.*

    To get Python working with Postgres, you will need to install the “psycopg2” module.

    pip install psycopg2

    now lets create a django project named geeks

    django-admin startproject geeks

    to check your django is running smoothly 

    python manage.py runserver

    Now, go to the below link and download and set up PostgreSQL. create a database name gfg in your Postgres server. Now its time to switch from SQLite to PostgreSQL.

    Folder structure -

    open the settings.py file 

    now change database settings with this template code

    DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.postgresql',
           'NAME': ‘<database_name>’,
           'USER': '<database_username>',
           'PASSWORD': '<password>',
           'HOST': '<database_hostname_or_ip>',
           'PORT': '<database_port>',
       }
    }

    Run these commands 

    python manage.py makemigrations
    python manage.py migrate

    now lets create the default superuser:

     python manage.py createsuperuser

    now again run your server with 

    python manage.py runserver

    go to this route and add the credential you did while creating superuser

    http://127.0.0.1:8000/admin/

    and if you are successfully able to log in, you have successfully switched to PostgreSQL

    Create Quiz

    J

    jagritacharya2019
    Improve

    J

    jagritacharya2019
    Improve
    Article Tags :
    • PostgreSQL
    • Python Django

    Explore

      Basics

      What is PostgreSQL - Introduction

      2 min read

      Install PostgreSQL on Windows

      2 min read

      Install PostgreSQL on Mac

      3 min read

      Database Operations

      PostgreSQL - Create Database

      5 min read

      PostgreSQL - Loading a Database

      3 min read

      PostgreSQL ALTER DATABASE

      3 min read

      PostgreSQL - Rename Database

      4 min read

      PostgreSQL - Show Databases

      3 min read

      Data Types

      PostgreSQL - Data Types

      5 min read

      PostgreSQL - Boolean Data Type

      4 min read

      PostgreSQL - CHAR Data Type

      5 min read

      PostgreSQL - VARCHAR Data Type

      3 min read

      PostgreSQL - NUMERIC Data Type

      5 min read

      PostgreSQL - Date Data Type

      4 min read

      PostgreSQL - TIME Data Type

      4 min read

      PostgreSQL - JSON Data Type

      4 min read

      PostgreSQL - CREATE DOMAIN

      3 min read

      Querying Tables

      PostgreSQL - SELECT

      3 min read

      PostgreSQL - ORDER BY clause

      2 min read

      PostgreSQL - WHERE clause

      6 min read

      PostgreSQL FETCH Clause

      4 min read

      PostgreSQL - IN operator

      4 min read

      PostgreSQL - HAVING clause

      4 min read

      PostgreSQL - GROUP BY clause

      4 min read

      PostgreSQL - LIKE operator

      5 min read

      PostgreSQL - BETWEEN Operator

      3 min read

      Table Operations

      PostgreSQL - CREATE TABLE

      5 min read

      PostgreSQL - SELECT INTO

      4 min read

      PostgreSQL - CREATE SEQUENCE

      4 min read

      PostgreSQL - ALTER TABLE

      6 min read

      PostgreSQL - ADD COLUMN

      4 min read

      PostgreSQL - DROP COLUMN

      2 min read

      PostgreSQL - Rename Table

      2 min read

      PostgreSQL - DROP TABLE

      5 min read

      PostgreSQL - TRUNCATE TABLE

      4 min read

      PostgreSQL - Copy a Table

      3 min read

      PostgreSQL - Comparing Tables

      3 min read

      PostgreSQL - Show Tables

      4 min read

      Modifying Data

      PostgreSQL - INSERT

      4 min read

      PostgreSQL - Insert Multiple Values in Various Rows

      3 min read

      PostgreSQL UPDATE Statement

      5 min read

      PostgreSQL - DELETE

      4 min read

      PostgreSQL - Upsert

      4 min read

      Conditionals

      PostgreSQL - CASE

      3 min read

      PostgreSQL COALESCE

      5 min read

      PostgreSQL - NULLIF() Function

      4 min read

      PostgreSQL - CAST

      3 min read

      Control Flow

      PostgreSQL - IF Statement

      5 min read

      PostgreSQL - CASE Statement

      4 min read

      PostgreSQL - Loop Statement

      3 min read

      PostgreSQL - While Loops

      4 min read

      PostgreSQL - Exit Statement

      3 min read

      PostgreSQL - Continue

      3 min read

      Transactions & Constraints

      PostgreSQL - Transactions

      4 min read

      PostgreSQL - COMMIT

      4 min read

      PostgreSQL - Primary Key

      4 min read

      PostgreSQL - Foreign Key

      5 min read

      PostgreSQL - CHECK Constraint

      2 min read

      PostgreSQL - UNIQUE Constraint

      3 min read

      PostgreSQL - NOT NULL Constraint

      3 min read

      JOINS & Schemas

      PostgreSQL - Joins

      5 min read

      PostgreSQL - LEFT JOIN

      5 min read

      PostgreSQL - INNER JOIN

      2 min read

      PostgreSQL - FULL OUTER JOIN

      4 min read

      PostgreSQL - SELF JOIN

      4 min read

      PostgreSQL - Schema

      5 min read

      PostgreSQL - CREATE SCHEMA

      5 min read

      PostgreSQL - DROP SCHEMA

      4 min read

      PostgreSQL - ALTER SCHEMA

      3 min read
    top_of_element && top_of_screen < bottom_of_element) || (bottom_of_screen > articleRecommendedTop && top_of_screen < articleRecommendedBottom) || (top_of_screen > articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • Software and Tools
  • Courses
  • ML and Data Science
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Interview Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences