Top Stories

How to connecting PostgreSQL with a Django application
Ankaj Gupta
April 13, 2021

How to connecting PostgreSQL with a Django application

Connect Django App to PostgreSQL Database

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

django connect with PostgreSQL

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 like PostgreSQL can significantly improve performance in production.

Why PostgreSQL? PostgreSQL offers better performance, advanced features, and robust data integrity for production applications.

Video Tutorial

Step-by-Step Guide

Step 1: Create PostgreSQL Database

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: Install psycopg2 Adapter

Install an adapter to connect the database with the Django app. We will be using psycopg2 for this purpose.

pip install psycopg2

Note: To install psycopg2 adapter, we also need to install Pillow if we wish to save any pictures in our database.

pip install pillow

Step 3: Configure Django Settings

Go to the settings.py file of your Django app. Under the settings file for databases, change the settings from SQLite or your current database to PostgreSQL.

Default SQLite Configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

PostgreSQL Configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'your-db-name',
        'USER': 'user_name',
        'PASSWORD': 'your-db-password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Important: Replace 'your-db-name', 'user_name', and 'your-db-password' with your actual PostgreSQL credentials.

Step 4: Run Makemigrations

Run the makemigrations command to convert all your models into database queries.

python manage.py makemigrations

Note: The correct command is python manage.py, not pip manage.py.

Step 5: Run Migrate

Finally, run the migrate command to migrate these changes into your PostgreSQL database.

python manage.py migrate

Verify Your Setup

You can verify the connection by checking your PostgreSQL database in pgAdmin:

Navigate to: Servers → PostgreSQL → Databases → DB_Name → Schema → Public → Tables

You should see Django's built-in tables created in your PostgreSQL database, confirming that the connection is successful!

Configuration Details

Setting Value Description
ENGINE django.db.backends.postgresql_psycopg2 Database backend adapter
NAME your-db-name Database name
USER user_name PostgreSQL username
PASSWORD your-db-password Database password
HOST localhost Database server hostname
PORT 5432 PostgreSQL default port

Benefits of Using PostgreSQL

Performance

Better performance for large-scale applications with concurrent users

Advanced Features

Support for complex queries, JSON data, and full-text search

Data Integrity

ACID compliance and robust data validation

Scalability

Handles large datasets and high traffic efficiently

Summary

Connecting Django to PostgreSQL is a straightforward process that involves installing the psycopg2 adapter, configuring your database settings in settings.py, and running migrations to create the necessary tables.

This setup provides a robust foundation for production Django applications, offering better performance, scalability, and advanced database features compared to the default SQLite database.

django PostgreSQL
Read
the pgadmin4 server could not contacted
Ankaj Gupta
March 20, 2021

the pgadmin4 server could not contacted

pgAdmin4 server could not be contacted

The Application Server Could Not Be Contacted (Embedded PostgreSQL)

Issue: After upgrading the embedded PostgreSQL database from version 9.4.22 to 10.9 in version 2019.11, pgAdmin4 fails to launch on Linux platform with the error: "The application server could not be contacted."

Cause

Compared to pgAdmin3 included in PostgreSQL database version 9.4.22, pgAdmin4 included in PostgreSQL database version 10.9 is a different client executable file, which could not be executed on Linux platform.

Solution

To enable launching the new pgAdmin4 client, a startup script was added, which then requires users to run the script in order to access the client.

For Data Flow Probe

To access the client of the embedded PostgreSQL database (version 10 or later), do the following:

  1. Run the following command to start the pgAdmin4 client:

    $UCMDB_HOME/pgsql/pgAdmin 4/bin/start_pgadmin4.sh
  2. Follow the on-screen instructions and access the local web browser using URL:

    http://127.0.0.1:5050
  3. Log in to the client

For UCMDB Server

To access the client of the embedded PostgreSQL database (version 10 or later), do the following:

  1. Run the following command to start the pgAdmin4 client:

    $DATAFLOW_PROBE_HOME/pgsql/pgAdmin 4/bin/start_pgadmin4.sh
  2. Follow the on-screen instructions and access the local web browser using URL:

    http://127.0.0.1:5050
  3. Log in to the client

Quick Reference

Environment Startup Script
Data Flow Probe $UCMDB_HOME/pgsql/pgAdmin 4/bin/start_pgadmin4.sh
UCMDB Server $DATAFLOW_PROBE_HOME/pgsql/pgAdmin 4/bin/start_pgadmin4.sh

Additional Notes

Version Compatibility

This issue specifically affects PostgreSQL versions 10.9 and later. Versions prior to 10.0 use pgAdmin3, which does not require this startup script.

Platform Specific

This solution is specifically for Linux platforms. Other operating systems may have different procedures for accessing pgAdmin4.

Access URL

Both environments use the same local URL: http://127.0.0.1:5050. Ensure no firewall is blocking this port.

PostgreSQL
Read