13

I'm trying to use contrib modules in the new Postgres 9 shared databases on Heroku. More specifically, the pg_trgm and fuzzystrmatch modules. In the documentation it says

In addition, many complimentary extensions are available such as fuzzystrmatch, pg_trgm, and unaccent.

I can't seem to find any documentation on HOW to actually enable these modules on a shared Heroku database. See answer below.

NOTE:

I tried adding them by connecting to the database with

heroku pg:psql HEROKU_POSTGRESQL_BROWN

and running

create extension pg_trgm
create extension fuzzystrmatch

but after trying to use it with

SELECT levenshtein('tests', 'test');

it still said

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.

Anybody know why this happens?

Constant Meiring
  • 3,285
  • 3
  • 40
  • 52

2 Answers2

17

Found answer here while scouring Stack Overflow. Don't know why it didn't come up in any of my Google searches. Going to leave the question here if anybody else uses the same wording to search for this.

To enable modules, you need to add them to a migration as follows:

def up
  execute "create extension fuzzystrmatch"
  execute "create extension pg_trgm"
end
Community
  • 1
  • 1
Constant Meiring
  • 3,285
  • 3
  • 40
  • 52
  • 2
    Note: You have to be on postgresql 9.1 for this to work. To upgrade from an older version do the following: dev (https://gist.github.com/2883249), prod (https://devcenter.heroku.com/articles/upgrading-postgres-versions) – jfeust Jul 18 '12 at 21:41
  • 2
    You can get a full listing of the available extensions on Heroku Postgres with `echo 'show extwlist.extensions' | heroku pg:psql` https://devcenter.heroku.com/articles/heroku-postgres-extensions-postgis-full-text-search – GregB Feb 19 '13 at 08:21
7

In newer versions of Rails it should be sufficient to do:

def change
  enable_extension "fuzzystrmatch"
  enable_extension "pg_trgm"
end

If you need to write up and down methods, the corresponding method to enable_extension is disable_extension.

Brian Hempel
  • 8,844
  • 2
  • 24
  • 19