Multiple Databases Django
Defining your databases
The first step to using more than one database with Django is to tell Django about the database servers you’ll be using. This is done using the DATABASES
setting. This setting maps database aliases, which are a way to refer to a specific database throughout Django, to a dictionary of settings for that specific connection. The settings in the inner dictionaries are described fully in the DATABASES
documentation.
Databases can have any alias you choose. However, the alias default
has special significance. Django uses the database with the alias of default
when no other database has been selected.
The following is an example settings.py
snippet defining two databases – a default PostgreSQL database and a MySQL database called users
:
DATABASES = {
“default”: {
“NAME”: “app_data”,
“ENGINE”: “django.db.backends.postgresql”,
“USER”: “postgres_user”,
“PASSWORD”: “skrit”,
},
“users”: {
“NAME”: “user_data”,
“ENGINE”: “django.db.backends.mysql”,
“USER”: “mysql_user”,
“PASSWORD”: “private”,
},
}