All notes

TIL: Postgres websearch_to_tsquery

BuddingPlanted Jun 29, 2026Last tended Jul 8, 2026postgressearchtil

TIL: Postgres websearch_to_tsquery

Postgres ships a full-text query parser that understands Google-like syntax out of the box — quoted phrases, OR, and -exclusions — no hand-rolled parser required:

SELECT title
FROM notes
WHERE search_vector @@ websearch_to_tsquery('english', '"row level security" OR rls -mysql');

Unlike to_tsquery, it never throws on user input: garbage in, empty result out. That makes it safe to wire a search box straight to it.

The setup is two lines of DDL — a generated column plus a GIN index:

ALTER TABLE notes ADD COLUMN search_vector tsvector
  GENERATED ALWAYS AS (to_tsvector('english', title || ' ' || body)) STORED;

CREATE INDEX notes_search_idx ON notes USING gin (search_vector);

This is the search engine planned for this very garden once it moves from static JSON to Postgres. Implementation notes live in Garden pipeline internals.

Linked from

Related notes

  • Never throw at module load in Next.js

    next build imports your modules with production NODE_ENV but none of your runtime env. A top-level throw on a missing env var kills the whole build. Degrade instead.

  • Two apps, one Neon database

    Sharing one Postgres between two apps works fine — if you prefix your tables and treat drizzle db:push as a loaded gun. Versioned migrations only.