I have a Django app and a Postgresql database (in production). Now I want to intall pg_trgm extension for Postgres. But I can't find any step-by-step instructions for installing it from Django app. I have a superuser status. How to do it correctly?
Asked
Active
Viewed 1,673 times
2
-
1Does this answer your question? [Error while configuring postgressql for django](https://stackoverflow.com/questions/64712167/error-while-configuring-postgressql-for-django) – Lord Elrond Dec 08 '20 at 01:59
-
But there are some difference with an official Django docs. I just want to be sure about my database. – Vit Amin Dec 08 '20 at 08:38
1 Answers
6
add 'django.contrib.postgres' in your INSTALLED_APPS
add a customer migration file in the app's migration folder. (The migration files are indexed, It's better to follow that index. e.g. 0044_customer_migrations.py)
add TrigramExtension in your migration file
from django.contrib.postgres.operations import TrigramExtension class Migration(migrations.Migration): dependencies = [ ('myapp', '0043_latest_migrations'), ] operations = [ TrigramExtension(), ]
run migrate
python manage.py migrate

Allen Shaw
- 1,164
- 7
- 23
-
this is the text from Django docs. But it is not clear: 1) should I create a new migration file? 2) where to put it? 3) what about dots in this code? – Vit Amin Dec 08 '20 at 08:36
-
1) python manage.py makemigration command will generate migration file for you when your model changes. 2) the file is in the migrations folder of your app. Edit that file, add the code there, then run python manage.py migrate. – Allen Shaw Dec 08 '20 at 08:53
-
but I dont want to make any database changes now. I just want to install pg_trgm – Vit Amin Dec 08 '20 at 09:01
-
1You can add migration file manually. I remove the dots code and add some steps in my answer for your reference. – Allen Shaw Dec 08 '20 at 09:53
-
Alas this produces errors: `psycopg2.errors.InsufficientPrivilege: permission denied to create extension "pg_trgm" HINT: Must be superuser to create this extension.` and it seems the solution requires either a SUPERUSER role or [Whitelisting](https://github.com/dimitri/pgextwlist]). – Bernd Wechner Nov 25 '22 at 10:11
-
installing any new extension requires super user privileges, which caused the errors for @BerndWechner, this is mentioned in django documentation as well – Yash Kumar Verma Jun 15 '23 at 11:53
-
So? It's still worth mentioning and including in the answer if an average reader is going to encounter a permission error. Alas dmitri removed the github gist I'd linked to, dang it. – Bernd Wechner Jun 16 '23 at 00:59