Skip to content

How to Connect PostgreSQL Database in Django"

PostgreSQL is an open-source, powerful, and widely used relational database management system (RDBMS), similar to MySQL. It allows you to store and manage data and supports advanced features like JSON support, full-text search, and more complex querying options.


How to Connect PostgreSQL Database in Django: A Step-by-Step Guide

To connect a PostgreSQL database to your Django project, follow these steps:

1. Install PostgreSQL and psycopg2

First, make sure PostgreSQL is installed on your system. If it isn't, you can install it using the appropriate method for your system:

  • On Ubuntu:

    sudo apt update
    sudo apt install postgresql postgresql-contrib
    
  • On Mac: You can use Homebrew:

    brew install postgresql
    

Next, install psycopg2, the PostgreSQL adapter for Python, which is required for Django to interact with PostgreSQL.

pip install psycopg2

Alternatively, you can install psycopg2-binary for simplicity:

pip install psycopg2-binary

2. Configure PostgreSQL

Once PostgreSQL is installed, create a database and a user for your Django application.

  1. Access the PostgreSQL prompt:

    sudo -u postgres psql
    
  2. Create a database:

    CREATE DATABASE mydatabase;
    
  3. Create a user with a password:

    CREATE USER myuser WITH PASSWORD 'mypassword';
    
  4. Grant all privileges on the database to the user:

    GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
    
  5. Exit the PostgreSQL prompt:

    \q
    

3. Update settings.py in Django

Now, update your Django project's settings.py to configure PostgreSQL as the database:

  1. Open your settings.py file.
  2. Modify the DATABASES setting to look like this:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',  # Replace with your database name
        'USER': 'myuser',      # Replace with your database user
        'PASSWORD': 'mypassword',  # Replace with your user password
        'HOST': 'localhost',   # Or the IP address if using a remote database
        'PORT': '5432',        # Default PostgreSQL port
    }
}

4. Apply Migrations

Once PostgreSQL is configured, run the following commands to apply migrations and create the necessary database tables:

python manage.py migrate

5. Run the Django Application

Now, you can run your Django application with PostgreSQL as the database:

python manage.py runserver

Your Django project is now connected to PostgreSQL!


Differences Between PostgreSQL and MySQL:

  • SQL Standard Compliance: PostgreSQL is considered more SQL-compliant and provides advanced features like foreign key constraints, window functions, and Common Table Expressions (CTEs).
  • Extensibility: PostgreSQL is known for its extensibility, allowing users to add custom functions, data types, and operators.
  • Data Integrity: PostgreSQL emphasizes data integrity and transactional reliability.
  • License: PostgreSQL uses a more permissive license, while MySQL is under Oracle's ownership with a different licensing model.

Installation and Running PostgreSQL

Unlike XAMPP (which bundles MySQL, Apache, etc.), PostgreSQL is usually installed separately. After installation, PostgreSQL runs as a service, similar to how MySQL runs in the background on XAMPP.

Steps to Install PostgreSQL:
  1. Install PostgreSQL:

    • On Linux (Ubuntu):

      sudo service postgresql start
      
    • On macOS (using Homebrew):

      brew services start postgresql
      
  2. Start the PostgreSQL Service:

    • On Linux:

      sudo service postgresql start
      
    • On macOS:

      brew services start postgresql
      
  3. Verify if PostgreSQL is running:

    • You can check if PostgreSQL is running using the command:

      sudo service postgresql status
      
    • Or use psql, the PostgreSQL command-line tool:

      psql -U postgres
      

Do You Need to Start a Server Manually?

Yes, just like MySQL in XAMPP, PostgreSQL runs as a server (or service) in the background. Once installed, it will automatically start when your machine boots up. You can start, stop, or restart PostgreSQL as needed.

In summary:
  • PostgreSQL is a standalone database server, like MySQL.
  • After installation, PostgreSQL runs as a service (you don't need a XAMPP-like tool).
  • You interact with PostgreSQL using its own command-line tool (psql) or through applications like Django, which connects to it once the service is running.