operations.txt 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. =============================
  2. Database migration operations
  3. =============================
  4. All of these :doc:`operations </ref/migration-operations>` are available from
  5. the ``django.contrib.postgres.operations`` module.
  6. .. _create-postgresql-extensions:
  7. Creating extension using migrations
  8. ===================================
  9. You can create a PostgreSQL extension in your database using a migration file.
  10. This example creates an hstore extension, but the same principles apply for
  11. other extensions.
  12. Set up the hstore extension in PostgreSQL before the first ``CreateModel``
  13. or ``AddField`` operation that involves
  14. :class:`~django.contrib.postgres.fields.HStoreField` by adding a migration with
  15. the :class:`~django.contrib.postgres.operations.HStoreExtension` operation.
  16. For example::
  17. from django.contrib.postgres.operations import HStoreExtension
  18. class Migration(migrations.Migration):
  19. ...
  20. operations = [
  21. HStoreExtension(),
  22. ...
  23. ]
  24. Creating the extension requires a database user with superuser privileges.
  25. If the Django database user doesn't have superuser privileges, you'll have
  26. to create the extension outside of Django migrations with a user that has
  27. the appropriate privileges. In that case, connect to your Django database and
  28. run the query ``CREATE EXTENSION IF NOT EXISTS hstore;``.
  29. .. currentmodule:: django.contrib.postgres.operations
  30. ``CreateExtension``
  31. ===================
  32. .. class:: CreateExtension(name)
  33. An ``Operation`` subclass which installs PostgreSQL extensions.
  34. .. attribute:: name
  35. This is a required argument. The name of the extension to be installed.
  36. ``BtreeGinExtension``
  37. =====================
  38. .. class:: BtreeGinExtension()
  39. .. versionadded:: 1.11
  40. Install the ``btree_gin`` extension.
  41. ``CITextExtension``
  42. ===================
  43. .. class:: CITextExtension()
  44. .. versionadded:: 1.11
  45. Installs the ``citext`` extension.
  46. ``HStoreExtension``
  47. ===================
  48. .. class:: HStoreExtension()
  49. Installs the ``hstore`` extension and also sets up the connection to
  50. interpret hstore data for possible use in subsequent migrations.
  51. ``TrigramExtension``
  52. ====================
  53. .. class:: TrigramExtension()
  54. Installs the ``pg_trgm`` extension.
  55. ``UnaccentExtension``
  56. =====================
  57. .. class:: UnaccentExtension()
  58. Installs the ``unaccent`` extension.