Databases sql Beginner

PostgreSQL Full-Text Search

Set up and query full-text search on a PostgreSQL table with ranking.

sql
ALTER TABLE modules ADD COLUMN search_vector tsvector
  GENERATED ALWAYS AS (to_tsvector('english', title || ' ' || description)) STORED;

CREATE INDEX idx_fts ON modules USING GIN(search_vector);

SELECT title, ts_rank(search_vector, query) AS rank
FROM modules, to_tsquery('english', 'nginx & deploy') query
WHERE search_vector @@ query
ORDER BY rank DESC;

Prerequisites

  • PostgreSQL >= 12

Pro Tips

  • → Use websearch_to_tsquery for user-input queries.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
sql
Level
Beginner
Read time
4 min
Published
Feb 2025
License
Commercial License

Newsletter

New modules every week, directly to your inbox.