<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang=""> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Database migration operations — Django 1.11.20 documentation</title> <link rel="stylesheet" href="../../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script> <script type="text/javascript" src="../../../_static/jquery.js"></script> <script type="text/javascript" src="../../../_static/underscore.js"></script> <script type="text/javascript" src="../../../_static/doctools.js"></script> <script type="text/javascript" src="../../../_static/language_data.js"></script> <link rel="index" title="Index" href="../../../genindex.html" /> <link rel="search" title="Search" href="../../../search.html" /> <link rel="next" title="Full text search" href="search.html" /> <link rel="prev" title="PostgreSQL specific lookups" href="lookups.html" /> <script type="text/javascript" src="../../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head><body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../../index.html">Django 1.11.20 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../../index.html">Home</a> | <a title="Table of contents" href="../../../contents.html">Table of contents</a> | <a title="Global index" href="../../../genindex.html">Index</a> | <a title="Module index" href="../../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="lookups.html" title="PostgreSQL specific lookups">previous</a> | <a href="../../index.html" title="API Reference" accesskey="U">up</a> | <a href="search.html" title="Full text search">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-contrib-postgres-operations"> <div class="section" id="s-database-migration-operations"> <span id="database-migration-operations"></span><h1>Database migration operations<a class="headerlink" href="#database-migration-operations" title="Permalink to this headline">¶</a></h1> <p>All of these <a class="reference internal" href="../../migration-operations.html"><span class="doc">operations</span></a> are available from the <code class="docutils literal notranslate"><span class="pre">django.contrib.postgres.operations</span></code> module.</p> <div class="section" id="s-creating-extension-using-migrations"> <span id="s-create-postgresql-extensions"></span><span id="creating-extension-using-migrations"></span><span id="create-postgresql-extensions"></span><h2>Creating extension using migrations<a class="headerlink" href="#creating-extension-using-migrations" title="Permalink to this headline">¶</a></h2> <p>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.</p> <p>Set up the hstore extension in PostgreSQL before the first <code class="docutils literal notranslate"><span class="pre">CreateModel</span></code> or <code class="docutils literal notranslate"><span class="pre">AddField</span></code> operation that involves <a class="reference internal" href="fields.html#django.contrib.postgres.fields.HStoreField" title="django.contrib.postgres.fields.HStoreField"><code class="xref py py-class docutils literal notranslate"><span class="pre">HStoreField</span></code></a> by adding a migration with the <a class="reference internal" href="#django.contrib.postgres.operations.HStoreExtension" title="django.contrib.postgres.operations.HStoreExtension"><code class="xref py py-class docutils literal notranslate"><span class="pre">HStoreExtension</span></code></a> operation. For example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.postgres.operations</span> <span class="k">import</span> <span class="n">HStoreExtension</span> <span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span> <span class="o">...</span> <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span> <span class="n">HStoreExtension</span><span class="p">(),</span> <span class="o">...</span> <span class="p">]</span> </pre></div> </div> <p>Creating the extension requires a database user with superuser privileges. If the Django database user doesn’t have superuser privileges, you’ll have to create the extension outside of Django migrations with a user that has the appropriate privileges. In that case, connect to your Django database and run the query <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">EXTENSION</span> <span class="pre">IF</span> <span class="pre">NOT</span> <span class="pre">EXISTS</span> <span class="pre">hstore;</span></code>.</p> </div> <div class="section" id="s-createextension"> <span id="createextension"></span><h2><code class="docutils literal notranslate"><span class="pre">CreateExtension</span></code><a class="headerlink" href="#createextension" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.postgres.operations.CreateExtension"> <em class="property">class </em><code class="descname">CreateExtension</code>(<em>name</em>)<a class="headerlink" href="#django.contrib.postgres.operations.CreateExtension" title="Permalink to this definition">¶</a></dt> <dd><p>An <code class="docutils literal notranslate"><span class="pre">Operation</span></code> subclass which installs PostgreSQL extensions.</p> <dl class="attribute"> <dt id="django.contrib.postgres.operations.CreateExtension.name"> <code class="descname">name</code><a class="headerlink" href="#django.contrib.postgres.operations.CreateExtension.name" title="Permalink to this definition">¶</a></dt> <dd><p>This is a required argument. The name of the extension to be installed.</p> </dd></dl> </dd></dl> </div> <div class="section" id="s-btreeginextension"> <span id="btreeginextension"></span><h2><code class="docutils literal notranslate"><span class="pre">BtreeGinExtension</span></code><a class="headerlink" href="#btreeginextension" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.postgres.operations.BtreeGinExtension"> <em class="property">class </em><code class="descname">BtreeGinExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.BtreeGinExtension" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.11.</span> </div> <p>Install the <code class="docutils literal notranslate"><span class="pre">btree_gin</span></code> extension.</p> </dd></dl> </div> <div class="section" id="s-citextextension"> <span id="citextextension"></span><h2><code class="docutils literal notranslate"><span class="pre">CITextExtension</span></code><a class="headerlink" href="#citextextension" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.postgres.operations.CITextExtension"> <em class="property">class </em><code class="descname">CITextExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.CITextExtension" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.11.</span> </div> <p>Installs the <code class="docutils literal notranslate"><span class="pre">citext</span></code> extension.</p> </dd></dl> </div> <div class="section" id="s-hstoreextension"> <span id="hstoreextension"></span><h2><code class="docutils literal notranslate"><span class="pre">HStoreExtension</span></code><a class="headerlink" href="#hstoreextension" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.postgres.operations.HStoreExtension"> <em class="property">class </em><code class="descname">HStoreExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.HStoreExtension" title="Permalink to this definition">¶</a></dt> <dd><p>Installs the <code class="docutils literal notranslate"><span class="pre">hstore</span></code> extension and also sets up the connection to interpret hstore data for possible use in subsequent migrations.</p> </dd></dl> </div> <div class="section" id="s-trigramextension"> <span id="trigramextension"></span><h2><code class="docutils literal notranslate"><span class="pre">TrigramExtension</span></code><a class="headerlink" href="#trigramextension" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.postgres.operations.TrigramExtension"> <em class="property">class </em><code class="descname">TrigramExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.TrigramExtension" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.10.</span> </div> <p>Installs the <code class="docutils literal notranslate"><span class="pre">pg_trgm</span></code> extension.</p> </dd></dl> </div> <div class="section" id="s-unaccentextension"> <span id="unaccentextension"></span><h2><code class="docutils literal notranslate"><span class="pre">UnaccentExtension</span></code><a class="headerlink" href="#unaccentextension" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.postgres.operations.UnaccentExtension"> <em class="property">class </em><code class="descname">UnaccentExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.UnaccentExtension" title="Permalink to this definition">¶</a></dt> <dd><p>Installs the <code class="docutils literal notranslate"><span class="pre">unaccent</span></code> extension.</p> </dd></dl> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../../../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Database migration operations</a><ul> <li><a class="reference internal" href="#creating-extension-using-migrations">Creating extension using migrations</a></li> <li><a class="reference internal" href="#createextension"><code class="docutils literal notranslate"><span class="pre">CreateExtension</span></code></a></li> <li><a class="reference internal" href="#btreeginextension"><code class="docutils literal notranslate"><span class="pre">BtreeGinExtension</span></code></a></li> <li><a class="reference internal" href="#citextextension"><code class="docutils literal notranslate"><span class="pre">CITextExtension</span></code></a></li> <li><a class="reference internal" href="#hstoreextension"><code class="docutils literal notranslate"><span class="pre">HStoreExtension</span></code></a></li> <li><a class="reference internal" href="#trigramextension"><code class="docutils literal notranslate"><span class="pre">TrigramExtension</span></code></a></li> <li><a class="reference internal" href="#unaccentextension"><code class="docutils literal notranslate"><span class="pre">UnaccentExtension</span></code></a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="lookups.html" title="previous chapter">PostgreSQL specific lookups</a></p> <h4>Next topic</h4> <p class="topless"><a href="search.html" title="next chapter">Full text search</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../../_sources/ref/contrib/postgres/operations.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Feb 11, 2019</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="lookups.html" title="PostgreSQL specific lookups">previous</a> | <a href="../../index.html" title="API Reference" accesskey="U">up</a> | <a href="search.html" title="Full text search">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>