<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stephen Appiah</title>
    <description>The latest articles on DEV Community by Stephen Appiah (@stevenwithph).</description>
    <link>https://dev.to/stevenwithph</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1069713%2F7d0bb352-cde3-420f-bcab-1aa249ed50fa.png</url>
      <title>DEV Community: Stephen Appiah</title>
      <link>https://dev.to/stevenwithph</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stevenwithph"/>
    <language>en</language>
    <item>
      <title>Installing Postgres Extensions with Django Migrations.</title>
      <dc:creator>Stephen Appiah</dc:creator>
      <pubDate>Tue, 25 Apr 2023 22:26:36 +0000</pubDate>
      <link>https://dev.to/stevenwithph/installing-postgres-extensions-with-django-migration-files-17a8</link>
      <guid>https://dev.to/stevenwithph/installing-postgres-extensions-with-django-migration-files-17a8</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D4GkiqbC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/12000/0%2Axumi7xxQ__-UJJPn" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D4GkiqbC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/12000/0%2Axumi7xxQ__-UJJPn" alt="Photo by [Tai Bui](https://unsplash.com/@agforlclassic?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s absolutely possible to create Postgres extensions without having to log into your database and run it manually. The same task can even be more daunting if you have to do it the Docker way. Django makes it way easier with &lt;a href="https://docs.djangoproject.com/en/3.2/ref/contrib/postgres/operations/"&gt;database migration operations&lt;/a&gt;. This way, you and your team members wouldn’t have to worry about extra DevOps work.&lt;/p&gt;

&lt;p&gt;In this example, we will learn how to create a &lt;strong&gt;pg_trgm&lt;/strong&gt; (or any other Postgres extension) by just creating a migration file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;django.contrib.postgres&lt;/code&gt; in &lt;code&gt;INSTALLED_APPS&lt;/code&gt; in your project settings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new migration file in your app’s migration folder. It’s conventional to follow the index of your last migration file. Say, your last migration file is &lt;code&gt;0012_last_migration_file.py&lt;/code&gt;, the new migration file could then be named, &lt;code&gt;0013_name_of_extension.py&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For installing, &lt;code&gt;pg_trgm&lt;/code&gt;, add the following code to the migration file you just created:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.contrib.postgres.operations&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TrigramExtension&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;migrations&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;migrations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'app_name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'0012_last_migration_file'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;operations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;TrigramExtension&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;TrigramExtension&lt;/code&gt; in lines 1 and 9 with any of the available Postgres operations subclasses. As at the time of writing these common extension subclasses are available:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BloomExtension&lt;/code&gt;, &lt;code&gt;BtreeGinExtension&lt;/code&gt;, &lt;code&gt;BtreeGistExtension&lt;/code&gt;, &lt;code&gt;CITextExtension&lt;/code&gt;, &lt;code&gt;CryptoExtension&lt;/code&gt;, &lt;code&gt;HStoreExtension&lt;/code&gt;, &lt;code&gt;TrigramExtension&lt;/code&gt;, &lt;code&gt;UnaccentExtension&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For extensions that are not listed above, use the &lt;code&gt;CreateExtension&lt;/code&gt; subclass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.contrib.postgres.operations&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;CreateExtension&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;migrations&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;migrations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'app_name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'last_migration_file'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;operations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;CreateExtension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'postgis'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;replace postgis in line 11 with the name of the extension to be installed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run migration, &lt;code&gt;python manage.py migrate&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Happy coding!
&lt;/h3&gt;

</description>
      <category>django</category>
      <category>postgres</category>
      <category>database</category>
    </item>
  </channel>
</rss>
