2016-01-03 10:56:22 +00:00
|
|
|
=============================
|
2014-03-14 17:34:49 +00:00
|
|
|
Database migration operations
|
|
|
|
=============================
|
|
|
|
|
|
|
|
All of these :doc:`operations </ref/migration-operations>` are available from
|
|
|
|
the ``django.contrib.postgres.operations`` module.
|
|
|
|
|
2016-11-05 13:18:11 +00:00
|
|
|
.. _create-postgresql-extensions:
|
|
|
|
|
|
|
|
Creating extension using migrations
|
|
|
|
===================================
|
|
|
|
|
|
|
|
You can create a PostgreSQL extension in your database using a migration file.
|
|
|
|
This example creates an hstore extension, but the same principles apply for
|
|
|
|
other extensions.
|
|
|
|
|
|
|
|
Set up the hstore extension in PostgreSQL before the first ``CreateModel``
|
|
|
|
or ``AddField`` operation that involves
|
|
|
|
:class:`~django.contrib.postgres.fields.HStoreField` by adding a migration with
|
|
|
|
the :class:`~django.contrib.postgres.operations.HStoreExtension` operation.
|
|
|
|
For example::
|
|
|
|
|
|
|
|
from django.contrib.postgres.operations import HStoreExtension
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
...
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
HStoreExtension(),
|
|
|
|
...
|
|
|
|
]
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
The operation skips adding the extension if it already exists.
|
2020-05-22 09:40:34 +00:00
|
|
|
|
2020-05-26 11:04:40 +00:00
|
|
|
For most extensions, this requires a database user with superuser privileges.
|
|
|
|
If the Django database user doesn't have the appropriate privileges, you'll
|
|
|
|
have to create the extension outside of Django migrations with a user that has
|
|
|
|
them. In that case, connect to your Django database and run the query
|
|
|
|
``CREATE EXTENSION IF NOT EXISTS hstore;``.
|
2016-11-05 13:18:11 +00:00
|
|
|
|
2020-05-22 09:40:34 +00:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
In older versions, the pre-existence of the extension isn't checked.
|
2020-05-22 09:40:34 +00:00
|
|
|
|
2014-03-14 17:34:49 +00:00
|
|
|
.. currentmodule:: django.contrib.postgres.operations
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``CreateExtension``
|
|
|
|
===================
|
2014-03-14 17:34:49 +00:00
|
|
|
|
|
|
|
.. class:: CreateExtension(name)
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
An ``Operation`` subclass which installs a PostgreSQL extension. For common
|
|
|
|
extensions, use one of the more specific subclasses below.
|
2014-03-14 17:34:49 +00:00
|
|
|
|
|
|
|
.. attribute:: name
|
|
|
|
|
|
|
|
This is a required argument. The name of the extension to be installed.
|
|
|
|
|
2019-10-18 09:08:50 +00:00
|
|
|
``BloomExtension``
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. class:: BloomExtension()
|
|
|
|
|
|
|
|
.. versionadded:: 3.1
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
Installs the ``bloom`` extension.
|
2019-10-18 09:08:50 +00:00
|
|
|
|
2016-08-08 03:15:08 +00:00
|
|
|
``BtreeGinExtension``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. class:: BtreeGinExtension()
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
Installs the ``btree_gin`` extension.
|
2016-08-08 03:15:08 +00:00
|
|
|
|
2017-07-01 13:30:34 +00:00
|
|
|
``BtreeGistExtension``
|
|
|
|
======================
|
|
|
|
|
|
|
|
.. class:: BtreeGistExtension()
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
Installs the ``btree_gist`` extension.
|
2017-07-01 13:30:34 +00:00
|
|
|
|
2016-06-01 21:43:59 +00:00
|
|
|
``CITextExtension``
|
|
|
|
===================
|
|
|
|
|
|
|
|
.. class:: CITextExtension()
|
|
|
|
|
|
|
|
Installs the ``citext`` extension.
|
|
|
|
|
2017-03-29 21:52:42 +00:00
|
|
|
``CryptoExtension``
|
|
|
|
===================
|
|
|
|
|
|
|
|
.. class:: CryptoExtension()
|
|
|
|
|
|
|
|
Installs the ``pgcrypto`` extension.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``HStoreExtension``
|
|
|
|
===================
|
2014-03-14 17:34:49 +00:00
|
|
|
|
|
|
|
.. class:: HStoreExtension()
|
|
|
|
|
2016-08-12 22:03:19 +00:00
|
|
|
Installs the ``hstore`` extension and also sets up the connection to
|
|
|
|
interpret hstore data for possible use in subsequent migrations.
|
2014-09-05 20:53:11 +00:00
|
|
|
|
2015-06-05 16:37:48 +00:00
|
|
|
``TrigramExtension``
|
|
|
|
====================
|
|
|
|
|
|
|
|
.. class:: TrigramExtension()
|
|
|
|
|
2016-08-12 22:03:19 +00:00
|
|
|
Installs the ``pg_trgm`` extension.
|
2015-06-05 16:37:48 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``UnaccentExtension``
|
|
|
|
=====================
|
2014-09-05 20:53:11 +00:00
|
|
|
|
|
|
|
.. class:: UnaccentExtension()
|
|
|
|
|
2016-08-12 22:03:19 +00:00
|
|
|
Installs the ``unaccent`` extension.
|
2019-07-25 11:44:18 +00:00
|
|
|
|
2020-09-30 12:00:59 +00:00
|
|
|
.. _manage-postgresql-collations:
|
|
|
|
|
|
|
|
Managing collations using migrations
|
|
|
|
====================================
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
If you need to filter or order a column using a particular collation that your
|
|
|
|
operating system provides but PostgreSQL does not, you can manage collations in
|
|
|
|
your database using a migration file. These collations can then be used with
|
|
|
|
the ``db_collation`` parameter on :class:`~django.db.models.CharField`,
|
|
|
|
:class:`~django.db.models.TextField`, and their subclasses.
|
|
|
|
|
|
|
|
For example, to create a collation for German phone book ordering::
|
|
|
|
|
|
|
|
from django.contrib.postgres.operations import CreateCollation
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
...
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
CreateCollation(
|
|
|
|
'german_phonebook',
|
|
|
|
provider='icu',
|
|
|
|
locale='und-u-ks-level2',
|
|
|
|
),
|
|
|
|
...
|
|
|
|
]
|
|
|
|
|
|
|
|
.. class:: CreateCollation(name, locale, *, provider='libc', deterministic=True)
|
|
|
|
|
|
|
|
Creates a collation with the given ``name``, ``locale`` and ``provider``.
|
|
|
|
|
|
|
|
Set the ``deterministic`` parameter to ``False`` to create a
|
|
|
|
non-deterministic collation, such as for case-insensitive filtering.
|
|
|
|
|
|
|
|
.. class:: RemoveCollation(name, locale, *, provider='libc', deterministic=True)
|
|
|
|
|
|
|
|
Removes the collations named ``name``.
|
|
|
|
|
|
|
|
When reversed this is creating a collation with the provided ``locale``,
|
|
|
|
``provider``, and ``deterministic`` arguments. Therefore, ``locale`` is
|
|
|
|
required to make this operation reversible.
|
|
|
|
|
|
|
|
.. admonition:: Restrictions
|
|
|
|
|
|
|
|
PostgreSQL 9.6 only supports the ``'libc'`` provider.
|
|
|
|
|
|
|
|
Non-deterministic collations are supported only on PostgreSQL 12+.
|
|
|
|
|
2020-06-23 09:31:46 +00:00
|
|
|
Concurrent index operations
|
2019-07-25 11:44:18 +00:00
|
|
|
===========================
|
|
|
|
|
|
|
|
PostgreSQL supports the ``CONCURRENTLY`` option to ``CREATE INDEX`` and
|
|
|
|
``DROP INDEX`` statements to add and remove indexes without locking out writes.
|
|
|
|
This option is useful for adding or removing an index in a live production
|
|
|
|
database.
|
|
|
|
|
|
|
|
.. class:: AddIndexConcurrently(model_name, index)
|
|
|
|
|
|
|
|
Like :class:`~django.db.migrations.operations.AddIndex`, but creates an
|
|
|
|
index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
|
|
|
|
of when using this option, see `the PostgreSQL documentation of building
|
|
|
|
indexes concurrently <https://www.postgresql.org/docs/current/
|
|
|
|
sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY>`_.
|
|
|
|
|
|
|
|
.. class:: RemoveIndexConcurrently(model_name, name)
|
|
|
|
|
|
|
|
Like :class:`~django.db.migrations.operations.RemoveIndex`, but removes the
|
|
|
|
index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
|
|
|
|
of when using this option, see `the PostgreSQL documentation
|
|
|
|
<https://www.postgresql.org/docs/current/sql-dropindex.html>`_.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The ``CONCURRENTLY`` option is not supported inside a transaction (see
|
|
|
|
:ref:`non-atomic migration <non-atomic-migrations>`).
|