我想使用Postgres的pg_trgm扩展来进行查询搜索。
当我运行此迁移时,它会给我以下错误:
SELECT * FROM USERS WHERE location like '%new%' ORDER BY location DESC;
由于Postgres已经没有pg_trgm,我需要执行一个命令来安装它。因此,我的迁移操作如下:
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
当我运行此迁移时,它会给我以下错误:
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);
如果我手动输入到数据库并执行命令,则此方法有效,但我需要从迁移中运行它。