Connect and Configure PostgreSQL in Django Project

Django, a popular web framework built on Python, provides excellent support for multiple databases, including PostgreSQL. PostgreSQL is a powerful open-source relational database management system that offers advanced features and scalability. In this blog, we’ll guide you through the process of connecting PostgreSQL to a Django project. By the end of this tutorial, you’ll be ready to use the full potential of Django with PostgreSQL.

Prerequisites:

Before we begin, ensure you have the following prerequisites in place:

  1. Django installed on your system.
  2. PostgreSQL database server installed and running.
  3. PostgreSQL database user with appropriate permissions.
  4. Basic familiarity with Django project structure and database concepts.
  5. Virtual Environment, this is optional but recommended. You check our blog here.

Note: For this tutorial, we are using our basic skeleton project for django. You can also download the project from here.

Step 1: Create the PostgreSQL Database

Make sure you have PostgreSQL installed and running. Create a new database for your Spring Boot application using the PostgreSQL interactive terminal or a GUI tool like pgAdmin. Note down the database name, username, and password, as we will need these credentials to connect from Spring Boot.

sudo -u postgres psql

CREATE DATABASE your_database_name;
CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;

\q

Step 2: Install the Required Packages

Start by installing the necessary packages to connect PostgreSQL to your Django project. Ensure you have psycopg2-binary, the PostgreSQL adapter for Python, installed. If not, you can install it using pip:

pip install psycopg2-binary

Step 3: Configure Database Postgres Settings

Open your Django project’s settings.py file (located inside the main project folder) and navigate to the DATABASES setting. By default, Django is configured to use SQLite, but we’ll change that to use PostgreSQL.

Replace the DATABASES setting with the following code:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'your_database_name',
        'USER': 'your_database_user',
        'PASSWORD': 'your_database_password',
        'HOST': 'localhost',  # Replace with your PostgreSQL server's address if necessary
        'PORT': '',          # Leave empty to use the default PostgreSQL port (usually 5432)
    }
}

Make sure to replace 'your_database_name', 'your_database_user', and 'your_database_password' with the appropriate values for your PostgreSQL database.

Step 4: Apply Django Migrations For Database Tables

Next, apply the initial database migrations to create the necessary tables in the PostgreSQL database. Run the following commands:

python manage.py makemigrations
python manage.py migrate

Step 5: Test the Connection

With all the configurations in place, it’s time to test the connection between Django and PostgreSQL. Start the Django development server:

python manage.py runserver

Visit http://127.0.0.1:8000/ in your web browser, and if everything is set up correctly, you should see the Django default landing page.

Conclusion:

Congratulations! You have successfully connected PostgreSQL to your Django project. PostgreSQL offers excellent performance, reliability, and scalability, making it an ideal choice for production-grade applications. You can now take full advantage of the advanced database features provided by PostgreSQL while building robust and high-performing web applications with Django.

Remember to keep your database credentials secure, especially when deploying your application to a production environment. Read our Blog on How to Protect Sensitive Data in Python Projects like Django and Flask.

Find this Project on Github.

Blogs you might like to Read!