How to use % operator from the extension pg_trgm?
Asked Answered
A

1

4

I have my pg_trgm module installed.

pg_trgm | 1.0     | extensions | text similarity measurement and index ...

The schema set is extensions. To use it I have to run something like this select:

extensions.similarity('hello','hallo');

I'm trying to run a statement using the % operator and got the following message.

mydb=# select * from rssdata where description % 'Brazil';
ERROR:  operator does not exist: character varying % unknown
LINE 1: select * from rssdata where description % 'Brazil';
                                            ^
HINT:  No operator matches the given name and argument type(s).
You might need to add explicit type casts. 

What is necessary to run % or <-> operators?

Amok answered 9/4, 2014 at 23:5 Comment(0)
R
9

Most probably the problem is with the search_path setting. Run:

SHOW search_path;

Is the schema where you installed pg_trgm included? If not, include it.

Or, if you have the necessary privileges, you can change the schema of an extension with:

ALTER EXTENSION pg_trgm SET SCHEMA public;  -- or the schema you want

Alternatively, you can schema-qualify functions - and even operators using the OPERATOR() construct:

SELECT * FROM rssdata WHERE extensions.similarity(description, 'Brazil') > .8;
SELECT * FROM rssdata WHERE description OPERATOR(extensions.%) 'Brazil';

Removes dependency on the search_path. But it's tedious.

Rebbeccarebe answered 9/4, 2014 at 23:9 Comment(4)
Thanks. I changed it to pg_catalog. ALTER EXTENSION pg_trgm SET SCHEMA pg_catalog; Seems that it works.Amok
@user3491961: But pg_catalog is a bad idea. This should be reserved for system objects. Install extensions to the public schema or to a dedicated extension schema and be sure to include the schema in the search_path. Do not abuse pg_catalog for that!Rebbeccarebe
Thanks. Started yesterday learning Postgresql. Thanks for this.Amok
create extentions within the schema: CREATE EXTENSION IF NOT EXISTS btree_gin WITH SCHEMA extensions; CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA extensions; should work after thisSoloma

© 2022 - 2024 — McMap. All rights reserved.