About Using Django Database URL

You can use dj-database-url to read database configuration from environment variable DATABASE_URL:

1
2
3
4
5
import dj_database_url

DATABASES["default"] = dj_database_url.config(
    conn_max_age=600, ssl_require=True,
)

Here are examples of DATABASE_URL definitions for different databases:

1
2
3
4
5
6
7
8
# PostgreSQL
export DATABASE_URL="postgres://username:password@localhost:5432/db_name"

# MySQL
export DATABASE_URL="mysql://username:password@localhost:3306/db_name"

# SQLite
export DATABASE_URL="sqlite:///absolute/path/to/db_name.sqlite3"

Tips and Tricks Programming Dev Ops Django 4.2 Django 3.2 Django 2.2