如何在Heroku Postgres数据库上启用contrib模块

Con*_*ing 12 postgresql ruby-on-rails heroku

我正在尝试在Heroku上的新Postgres 9共享数据库中使用contrib模块.更具体地说,是pg_trgm和fuzzystrmatch模块.在文档中说

此外,还提供许多免费扩展,例如fuzzystrmatch,pg_trgm和unaccent.

我似乎无法找到关于如何在共享的Heroku数据库上实际启用这些模块的任何文档.见下面的答案.

注意:

我尝试通过连接数据库来添加它们

heroku pg:psql HEROKU_POSTGRESQL_BROWN
Run Code Online (Sandbox Code Playgroud)

并运行

create extension pg_trgm
create extension fuzzystrmatch
Run Code Online (Sandbox Code Playgroud)

但在尝试使用它之后

SELECT levenshtein('tests', 'test');
Run Code Online (Sandbox Code Playgroud)

它仍然说

ERROR:  function levenshtein(unknown, unknown) does not existLINE 1: SELECT levenshtein('tests', 'test');
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)

谁知道为什么会这样?

Con*_*ing 16

在搜索 Stack Overflow的同时找到答案.不知道为什么我的Google搜索没有出现.如果其他人使用相同的措辞来搜索这个问题,那么就在这里留下问题.

要启用模块,您需要将它们添加到迁移中,如下所示:

def up
  execute "create extension fuzzystrmatch"
  execute "create extension pg_trgm"
end
Run Code Online (Sandbox Code Playgroud)

  • 注意:您必须使用postgresql 9.1才能使用此功能.要从旧版本升级,请执行以下操作:dev(https://gist.github.com/2883249),prod(https://devcenter.heroku.com/articles/upgrading-postgres-versions) (2认同)
  • 您可以使用`echo'show extwlist.extensions'获取Heroku Postgres上可用扩展的完整列表.heroku pg:psql` https://devcenter.heroku.com/articles/heroku-postgres-extensions-postgis-full-text-search (2认同)

Bri*_*pel 7

在较新版本的Rails中,它应该足够:

def change
  enable_extension "fuzzystrmatch"
  enable_extension "pg_trgm"
end
Run Code Online (Sandbox Code Playgroud)

如果需要编写up和down方法,相应的方法enable_extension就是disable_extension.