Question: psycopg2.errors.UndefinedObject: operator class "gin_trgm_ops" does not exist for access method "gin" Hello everybody, this is the whole message when I try to run pytest on my project which is written in Python/Django + db is postgresql and all sitting inside docker. I build a project on docker with django-cookiecutter template, all settings are default. I put a gin index to one of my string fields, migrations running successfully, pg_trgm extention is being created successfully, but if I try to test my project with pytest I got this error. Here is my pytest.ini [pytest] DJANGO_SETTINGS_MODULE = config.settings.test  Here is my test settings file's database configuration: test.py DATABASES['test'] = { # noqa 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'test', 'PASSWORD': 'test', 'USER': 'test', 'HOST': 'localhost', 'PORT': 5454, }  This is part of the migration which creates the extention pg_trgm and puts an index to the field given migrations.AddIndex( model_name='<model_name>', index=django.contrib.postgres.indexes.GinIndex(fields=['field_name'], name='field_name_gin_idx', opclasses=['gin_trgm_ops']), ),  And this is the whole traceback which I am getting: self = <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0> sql = 'CREATE INDEX "bank_name_gin_idx" ON "financing_graincreditagrobankworksheet" USING gin ("bank_name" gin_trgm_ops)', params = None ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0xffff7d096b80>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0>}) def _execute(self, sql, params, *ignored_wrapper_args): self.db.validate_no_broken_transaction() with self.db.wrap_database_errors: if params is None: # params default might be backend specific. > return self.cursor.execute(sql) E psycopg2.errors.UndefinedObject: operator class "gin_trgm_ops" does not exist for access method "gin" /usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:82: UndefinedObject The above exception was the direct cause of the following exception: request = <SubRequest '_django_setup_unittest' for <TestCaseFunction test_correct_insurance_company_name>> django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0xffff7fe702b0> @pytest.fixture(autouse=True, scope="class") def _django_setup_unittest( request, django_db_blocker: "_DatabaseBlocker", ) -> Generator[None, None, None]: """Setup a django unittest, internal to pytest-django.""" if not django_settings_is_configured() or not is_django_unittest(request): yield return # Fix/patch pytest. # Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991 # After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824 from _pytest.unittest import TestCaseFunction original_runtest = TestCaseFunction.runtest def non_debugging_runtest(self) -> None: self._testcase(result=self) try: TestCaseFunction.runtest = non_debugging_runtest # type: ignore[assignment] > request.getfixturevalue("django_db_setup") /usr/local/lib/python3.9/site-packages/pytest_django/plugin.py:490: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /usr/local/lib/python3.9/site-packages/pytest_django/fixtures.py:122: in django_db_setup db_cfg = setup_databases( /usr/local/lib/python3.9/site-packages/django/test/utils.py:179: in setup_databases connection.creation.create_test_db( /usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py:74: in create_test_db call_command( /usr/local/lib/python3.9/site-packages/django/core/management/__init__.py:181: in call_command return command.execute(*args, **defaults) /usr/local/lib/python3.9/site-packages/django/core/management/base.py:398: in execute output = self.handle(*args, **options) /usr/local/lib/python3.9/site-packages/django/core/management/base.py:89: in wrapped res = handle_func(*args, **kwargs) /usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py:244: in handle post_migrate_state = executor.migrate( /usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py:117: in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) /usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py:147: in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) /usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py:227: in apply_migration state = migration.apply(state, schema_editor) /usr/local/lib/python3.9/site-packages/django/db/migrations/migration.py:126: in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) /usr/local/lib/python3.9/site-packages/django/db/migrations/operations/models.py:761: in database_forwards schema_editor.add_index(model, self.index) /usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py:218: in add_index self.execute(index.create_sql(model, self, concurrently=concurrently), params=None) /usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py:145: in execute cursor.execute(sql, params) /usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:66: in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) /usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:75: in _execute_with_wrappers return executor(sql, params, many, context) /usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:84: in _execute return self.cursor.execute(sql, params) /usr/local/lib/python3.9/site-packages/django/db/utils.py:90: in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0> sql = 'CREATE INDEX "bank_name_gin_idx" ON "financing_graincreditagrobankworksheet" USING gin ("bank_name" gin_trgm_ops)', params = None ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0xffff7d096b80>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0>}) def _execute(self, sql, params, *ignored_wrapper_args): self.db.validate_no_broken_transaction() with self.db.wrap_database_errors: if params is None: # params default might be backend specific. > return self.cursor.execute(sql) E django.db.utils.ProgrammingError: operator class "gin_trgm_ops" does not exist for access method "gin" /usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:82: ProgrammingError  I want to run tests with pytest and got this problem which is actually not causing any problem while project is running, gin index is actually working. But only when I run a test no matter its django's manage.py test or pytest it fails with the error above. If I comment out the part of migration which should generate an index it works.

Answer: Try adding to your migration: from django.contrib.postgres.operations import TrigramExtension operations = [ TrigramExtension(), ... ]