pg_trgm Similarity Calculator
The pg_trgm extension powers fuzzy search in PostgreSQL. It splits text into trigrams (groups of three characters) and scores how many two strings share. The scores are easy to misread. similarity('postgres', 'postgresql') is 0.6666667, not 0.9, because the extra letters add trigrams. 'MySQL' and 'my sql' differ because the space starts a new word. A search term that matches one word inside a long value scores low with similarity() and high with word_similarity(). This calculator runs the same algorithm as pg_trgm. It lowercases the text, splits it into words of letters and digits, pads each word with two spaces in front and one behind, and hashes trigrams that contain non-ASCII characters. Enter a search term and the values you expect it to find. You get show_trgm() for each string, the shared trigrams and the exact similarity, word_similarity and strict_word_similarity values, printed the way PostgreSQL prints a real. It also shows the <->, <<-> and <<<-> distances and whether %, <% and <<% match at your thresholds. Use it to choose pg_trgm.similarity_threshold before you create a GIN index. Results were checked against PostgreSQL 18.6 with 12,000 random string pairs (English, accented, Greek, Cyrillic, CJK and emoji text) and matched every value. Everything runs in your browser.
Do more than pg_trgm similarity calculator — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Type the search term exactly as users would enter it, and paste the values it should (or should not) find, one per line.
- Keep the default thresholds (0.3, 0.6 and 0.5) or enter the ones your database uses (SHOW pg_trgm.similarity_threshold), then click Calculate.
- Compare the scores and the %, <% and <<% columns, read the trigram breakdown for surprising results, and copy the SQL with the threshold that works.
Frequently asked questions
Why does similarity() return a low score for an obvious match?
similarity() divides the shared trigrams by all distinct trigrams of both strings (shared / (a + b - shared)), so every extra character in either string lowers it. 'postgres' and 'postgresql' share 8 trigrams out of 12, giving 0.6666667. A short term inside a longer value scores low too: similarity('oracle', 'Oracle Database 23ai Free') is 0.26923078, below the default 0.3, so % does not match. word_similarity('oracle', 'Oracle Database 23ai Free') is 1, because it compares the term with the best-matching part of the value, so use it with the <% operator for that kind of search. strict_word_similarity (<<%) does the same but only uses whole-word boundaries.
How do I change the pg_trgm similarity threshold?
The % operator matches when similarity is at least pg_trgm.similarity_threshold, which defaults to 0.3. Change it for the session with SET pg_trgm.similarity_threshold = 0.4, for a role or database with ALTER ROLE ... SET or ALTER DATABASE ... SET, or in postgresql.conf. The older set_limit(0.4) and show_limit() functions still work but are deprecated. <% uses pg_trgm.word_similarity_threshold (default 0.6) and <<% uses pg_trgm.strict_word_similarity_threshold (default 0.5). Values must be between 0 and 1.
Does pg_trgm work with accents, Chinese or Japanese text?
Words are runs of letters and digits as defined by the database's LC_CTYPE, so in a UTF-8 database accented, Greek, Cyrillic and CJK characters count as word characters and are lowercased. Trigrams that contain a multibyte character are stored as a 3-byte hash, which show_trgm prints as 0x followed by six hex digits. Accents are not removed, so 'café' and 'cafe' only share some trigrams. Combine pg_trgm with the unaccent extension if you need accent-insensitive search. Chinese and Japanese text has no spaces, so a whole sentence becomes one long word and short searches score low. You can try these queries against your own tables in Chat2DB: download it at chat2db.ai/download or open app.chat2db.ai.
