Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable contrib modules on Heroku Postgres database

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?

like image 727
Constant Meiring Avatar asked May 25 '12 10:05

Constant Meiring


2 Answers

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
like image 168
Constant Meiring Avatar answered Nov 07 '22 04:11

Constant Meiring


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.

like image 21
Brian Hempel Avatar answered Nov 07 '22 05:11

Brian Hempel