Similarity function in Postgres with pg_trgm
Asked Answered
B

7

28

I'm trying to use the similarity function in Postgres to do some fuzzy text matching, however whenever I try to use it I get the error:

function similarity(character varying, unknown) does not exist

If I add explicit casts to text I get the error:

function similarity(text, text) does not exist

My query is:

SELECT (similarity("table"."field"::text, %s::text)) AS "similarity", "table".* FROM "table" WHERE similarity > .5 ORDER BY "similarity" DESC LIMIT 10

Do I need to do something to initalize pg_trgm?

Branca answered 12/2, 2010 at 20:44 Comment(0)
P
10

You have to install pg_trgm. In debian, source this sql: /usr/share/postgresql/8.4/contrib/pg_trgm.sql. From the command line:

psql -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

Or inside a psql shell:

\i /usr/share/postgresql/8.4/contrib/pg_trgm.sql

The script defaults to installing in the public schema, edit the search path at the top if you want to install it somewhere else (so that uninstalling/upgrading can be done simply by dropping the schema).

Postorbital answered 12/2, 2010 at 20:54 Comment(1)
Possibly really dumb question, but how does one do that?Branca
V
70

With postgresql 9.1:

after installing (on ubuntu) sudo apt-get install postgresql-contrib as tomaszbak answered.

you just have to execute the sql command:

CREATE EXTENSION pg_trgm;
Vorster answered 14/5, 2013 at 20:50 Comment(2)
It may be worth to remember that depending on database search_path extension could be installed in different schema than public.Gragg
note: since you have to be superuser, go to the commandline and select your database psql --username postgres --dbname database and run the command there. Just in default database won't work for your production appLysippus
P
10

You have to install pg_trgm. In debian, source this sql: /usr/share/postgresql/8.4/contrib/pg_trgm.sql. From the command line:

psql -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

Or inside a psql shell:

\i /usr/share/postgresql/8.4/contrib/pg_trgm.sql

The script defaults to installing in the public schema, edit the search path at the top if you want to install it somewhere else (so that uninstalling/upgrading can be done simply by dropping the schema).

Postorbital answered 12/2, 2010 at 20:54 Comment(1)
Possibly really dumb question, but how does one do that?Branca
G
8

On ubuntu you need to run

sudo apt-get install postgresql-contrib

to get /usr/share/postgresql/8.4/contrib/pg_trgm.sql

Gershom answered 27/10, 2011 at 13:32 Comment(1)
For postgres 8.4. contrib run sudo apt-get install postgresql-contrib-8.4Fundament
E
6

If you have the pg_trgm extension installed not in the public schema you must explicitly specify the schema when using the similarity function like this

select schema.similarity(foo,bar) from schema.baz
Enosis answered 12/7, 2014 at 9:55 Comment(1)
How do I find out which schema has it installed?Pituri
F
2

For Postgres 8.4 do following:

As sudo user run:

sudo apt-get install postgresql-contrib-8.4

Switch to postgres user:

sudo su - postgres

Run:

psql -U DB_USER -d DB_NAME -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

Restart postgres

Fundament answered 6/11, 2014 at 12:18 Comment(0)
O
0

I was having this same issue in the context of running the Django Test Runner against a function that uses the Django 1.11 ORM for trigram similarity on Postgres 9.4.

I had to do a few things to get it working:

1) OP is correct that this required enabling the pg_trgm extension. However, in postgres9.4 this is enabled on a per-database basis. Since Django deletes and recreates the test database with each run, the new test database didn't have the extension installed. To fix this, I initialized the pg_trgm extension within the default newly-created database template in postgres. The command to do this is psql -d template1 -c 'CREATE EXTENSION pg_trgm;' run as the postgres user.

2) Postgres had to be restarted

3) The Django test runner wasn't recognizing this, so I had to upgrade from Django 1.11.12 to 1.11.18 (presumably this is also fixed in newer versions of Django)

Oolite answered 24/1, 2019 at 18:16 Comment(0)
C
0

in case others are struggling too: assuming that the db is called “ttrss” I switched to the command line of postgresql:

sudo -u postgres psql --dbname=ttrss

then install the extension there:

CREATE EXTENSION pg_trgm;
Chromyl answered 29/9, 2021 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.