Databases¶
Django officially supports the following databases:
There are also a number of database backends provided by third parties.
Django attempts to support as many features as possible on all database backends. However, not all database backends are alike, and we’ve had to make design decisions on which features to support and which assumptions we can make safely.
This file describes some of the features that might be relevant to Django usage. It is not intended as a replacement for server-specific documentation or reference manuals.
General notes¶
Persistent connections¶
Persistent connections avoid the overhead of re-establishing a connection to
the database in each request. They’re controlled by the
CONN_MAX_AGE
parameter which defines the maximum lifetime of a
connection. It can be set independently for each database.
The default value is 0
, preserving the historical behavior of closing the
database connection at the end of each request. To enable persistent
connections, set CONN_MAX_AGE
to a positive integer of seconds. For
unlimited persistent connections, set it to None
.
Connection management¶
Django opens a connection to the database when it first makes a database
query. It keeps this connection open and reuses it in subsequent requests.
Django closes the connection once it exceeds the maximum age defined by
CONN_MAX_AGE
or when it isn’t usable any longer.
In detail, Django automatically opens a connection to the database whenever it needs one and doesn’t have one already — either because this is the first connection, or because the previous connection was closed.
At the beginning of each request, Django closes the connection if it has
reached its maximum age. If your database terminates idle connections after
some time, you should set CONN_MAX_AGE
to a lower value, so that
Django doesn’t attempt to use a connection that has been terminated by the
database server. (This problem may only affect very low traffic sites.)
At the end of each request, Django closes the connection if it has reached its maximum age or if it is in an unrecoverable error state. If any database errors have occurred while processing the requests, Django checks whether the connection still works, and closes it if it doesn’t. Thus, database errors affect at most one request; if the connection becomes unusable, the next request gets a fresh connection.
Caveats¶
Since each thread maintains its own connection, your database must support at least as many simultaneous connections as you have worker threads.
Sometimes a database won’t be accessed by the majority of your views, for
example because it’s the database of an external system, or thanks to caching.
In such cases, you should set CONN_MAX_AGE
to a low value or even
0
, because it doesn’t make sense to maintain a connection that’s unlikely
to be reused. This will help keep the number of simultaneous connections to
this database small.
The development server creates a new thread for each request it handles, negating the effect of persistent connections. Don’t enable them during development.
When Django establishes a connection to the database, it sets up appropriate parameters, depending on the backend being used. If you enable persistent connections, this setup is no longer repeated every request. If you modify parameters such as the connection’s isolation level or time zone, you should either restore Django’s defaults at the end of each request, force an appropriate value at the beginning of each request, or disable persistent connections.
Encoding¶
Django assumes that all databases use UTF-8 encoding. Using other encodings may result in unexpected behavior such as “value too long” errors from your database for data that is valid in Django. See the database specific notes below for information on how to set up your database correctly.
PostgreSQL notes¶
Django supports PostgreSQL 9.5 and higher. psycopg2 2.5.4 or higher is required, though the latest release is recommended.
Optimizing PostgreSQL’s configuration¶
Django needs the following parameters for its database connections:
client_encoding
:'UTF8'
,default_transaction_isolation
:'read committed'
by default, or the value set in the connection options (see below),
If these parameters already have the correct values, Django won’t set them for
every new connection, which improves performance slightly. You can configure
them directly in postgresql.conf
or more conveniently per database
user with ALTER ROLE.
Django will work just fine without this optimization, but each new connection will do some additional queries to set these parameters.
Isolation level¶
Like PostgreSQL itself, Django defaults to the READ COMMITTED
isolation
level. If you need a higher isolation level such as REPEATABLE READ
or
SERIALIZABLE
, set it in the OPTIONS
part of your database
configuration in DATABASES
:
import psycopg2.extensions
DATABASES = {
# ...
'OPTIONS': {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
},
}
Note
Under higher isolation levels, your application should be prepared to handle exceptions raised on serialization failures. This option is designed for advanced uses.
Indexes for varchar
and text
columns¶
When specifying db_index=True
on your model fields, Django typically
outputs a single CREATE INDEX
statement. However, if the database type
for the field is either varchar
or text
(e.g., used by CharField
,
FileField
, and TextField
), then Django will create
an additional index that uses an appropriate PostgreSQL operator class
for the column. The extra index is necessary to correctly perform
lookups that use the LIKE
operator in their SQL, as is done with the
contains
and startswith
lookup types.
Migration operation for adding extensions¶
If you need to add a PostgreSQL extension (like hstore
, postgis
, etc.)
using a migration, use the
CreateExtension
operation.
Server-side cursors¶
When using QuerySet.iterator()
, Django opens a server-side
cursor. By default, PostgreSQL assumes that
only the first 10% of the results of cursor queries will be fetched. The query
planner spends less time planning the query and starts returning results
faster, but this could diminish performance if more than 10% of the results are
retrieved. PostgreSQL’s assumptions on the number of rows retrieved for a
cursor query is controlled with the cursor_tuple_fraction option.
Transaction pooling and server-side cursors¶
Using a connection pooler in transaction pooling mode (e.g. PgBouncer) requires disabling server-side cursors for that connection.
Server-side cursors are local to a connection and remain open at the end of a
transaction when AUTOCOMMIT
is True
. A
subsequent transaction may attempt to fetch more results from a server-side
cursor. In transaction pooling mode, there’s no guarantee that subsequent
transactions will use the same connection. If a different connection is used,
an error is raised when the transaction references the server-side cursor,
because server-side cursors are only accessible in the connection in which they
were created.
One solution is to disable server-side cursors for a connection in
DATABASES
by setting DISABLE_SERVER_SIDE_CURSORS
to True
.
To benefit from server-side cursors in transaction pooling mode, you could set up another connection to the database in order to perform queries that use server-side cursors. This connection needs to either be directly to the database or to a connection pooler in session pooling mode.
Another option is to wrap each QuerySet
using server-side cursors in an
atomic()
block, because it disables autocommit
for the duration of the transaction. This way, the server-side cursor will only
live for the duration of the transaction.
Manually-specifying values of auto-incrementing primary keys¶
Django uses PostgreSQL’s SERIAL data type to store auto-incrementing primary
keys. A SERIAL
column is populated with values from a sequence that
keeps track of the next available value. Manually assigning a value to an
auto-incrementing field doesn’t update the field’s sequence, which might later
cause a conflict. For example:
>>> from django.contrib.auth.models import User
>>> User.objects.create(username='alice', pk=1)
<User: alice>
>>> # The sequence hasn't been updated; its next value is 1.
>>> User.objects.create(username='bob')
...
IntegrityError: duplicate key value violates unique constraint
"auth_user_pkey" DETAIL: Key (id)=(1) already exists.
If you need to specify such values, reset the sequence afterwards to avoid
reusing a value that’s already in the table. The sqlsequencereset
management command generates the SQL statements to do that.
Test database templates¶
You can use the TEST['TEMPLATE']
setting to specify
a template (e.g. 'template0'
) from which to create a test database.
Speeding up test execution with non-durable settings¶
You can speed up test execution times by configuring PostgreSQL to be non-durable.
Warning
This is dangerous: it will make your database more susceptible to data loss or corruption in the case of a server crash or power loss. Only use this on a development machine where you can easily restore the entire contents of all databases in the cluster.
MariaDB notes¶
Django supports MariaDB 10.2 and higher.
To use MariaDB, use the MySQL backend, which is shared between the two. See the MySQL notes for more details.
MySQL notes¶
Version support¶
Django supports MySQL 5.6 and higher.
Django’s inspectdb
feature uses the information_schema
database, which
contains detailed data on all database schemas.
Django expects the database to support Unicode (UTF-8 encoding) and delegates to it the task of enforcing transactions and referential integrity. It is important to be aware of the fact that the two latter ones aren’t actually enforced by MySQL when using the MyISAM storage engine, see the next section.
Storage engines¶
MySQL has several storage engines. You can change the default storage engine in the server configuration.
MySQL’s default storage engine is InnoDB. This engine is fully transactional
and supports foreign key references. It’s the recommended choice. However, the
InnoDB autoincrement counter is lost on a MySQL restart because it does not
remember the AUTO_INCREMENT
value, instead recreating it as “max(id)+1”.
This may result in an inadvertent reuse of AutoField
values.
The main drawbacks of MyISAM are that it doesn’t support transactions or enforce foreign-key constraints.
MySQL DB API Drivers¶
MySQL has a couple drivers that implement the Python Database API described in PEP 249:
- mysqlclient is a native driver. It’s the recommended choice.
- MySQL Connector/Python is a pure Python driver from Oracle that does not require the MySQL client library or any Python modules outside the standard library.
These drivers are thread-safe and provide connection pooling.
In addition to a DB API driver, Django needs an adapter to access the database drivers from its ORM. Django provides an adapter for mysqlclient while MySQL Connector/Python includes its own.
mysqlclient¶
Django requires mysqlclient 1.4.0 or later.
MySQL Connector/Python¶
MySQL Connector/Python is available from the download page. The Django adapter is available in versions 1.1.X and later. It may not support the most recent releases of Django.
Time zone definitions¶
If you plan on using Django’s timezone support, use mysql_tzinfo_to_sql to load time zone tables into the MySQL database. This needs to be done just once for your MySQL server, not per database.