I want to use pg_trgm extension of Postgres for searching using query
SELECT * FROM USERS WHERE location like '%new%' ORDER BY location DESC;
Since Postgres already don't have pg_trgm I need to execute a command to install it. So my migration to do so is
class Addtrigramindexlocationtousers < ActiveRecord::Migration[5.1]
def change
reversible do |direction|
direction.up {
execute %{
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX index_users_trigram_on_location ON users USING gin (location gin_trgm_ops);
}
}
direction.down {
execute %{
DROP INDEX index_users_trigram_on_location;
}
}
end
end
end
So when I run this migration it is giving me this error:
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied to create extension "pg_trgm"
HINT: Must be superuser to create this extension.
:
DROP INDEX index_users_on_location;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX index_users_trigram_on_location ON users USING gin (location gin_trgm_ops);
This works if I manually enter to database and execute the command but I need to run it from migration.