๐Ÿ“ Django

Databases in Django

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
257
๐ŸŒฑ
Level
Beginner

Covered topics: Supported Database Backends, Configuring SQLite (default), Configuring PostgreSQL, Via environment variable (recommended).

Supported Database Backends

Django supports several databases out of the box:

Database Package Use case
SQLite built-in development
PostgreSQL psycopg production
MySQL/MariaDB mysqlclient production
Oracle oracledb enterprise

Configuring SQLite (default)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

Configuring PostgreSQL

pip install "psycopg[binary]"
import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': os.environ['POSTGRES_PASSWORD'],
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
pip install dj-database-url
import dj_database_url

DATABASES = {
    'default': dj_database_url.config(
        default='sqlite:///db.sqlite3',
        conn_max_age=600,
    )
}

.env:

DATABASE_URL=postgresql://user:pass@localhost/mydb

Multiple databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'app',
    },
    'analytics': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'analytics',
    },
}
# Usage
Task.objects.using('analytics').all()

Why PostgreSQL for production

  • Full transaction support
  • JSON fields (JSONField)
  • Full-text search
  • Reliability and scalability

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Django ORM: Cheatsheet

ORM (Object-Relational Mapping) lets you work with the database through Python objects without writing SQL.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 311
๐Ÿ“

Date Formats in Django

Covered topics: Date fields in a model, Working with dates in Python, Formatting in templates,...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 328
๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 277
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum