Postgres as a Search Engine

Eric Zakariasson6 Mar 2025

Problem

Teams that need search for an app or a RAG pipeline usually reach for a separate search service. Keyword search alone misses meaning, and vector search alone struggles with names, abbreviations and typos. The article asks how far an existing Postgres database can go. Search quality decides the quality of the whole RAG pipeline, so the retrieval layer has to be right first.

Example from the article: a documents table with a title, a body and a 1536-dimensional embedding, searched by one query that combines three signals.

Solution in brief

Run three retrievers in Postgres: full-text search on tsvector, fuzzy matching with pg_trgm, and vector search with pgvector. Each retriever returns its top 30 as ranks. One SQL query merges the ranks with Reciprocal Rank Fusion (RRF) and per-method weights. Return every sub-score as JSON for debugging, and add recency or user boosts if the use case needs them.

Questions this case answers
  • How to combine lexical, typo-tolerant and embedding-based retrieval into one ranking when the data already sits in a relational database and a dedicated search engine is not wanted
  • How to make a multi-signal ranking explainable and tunable by weights without retraining a model
  • How to build hybrid search (full-text, fuzzy and semantic) inside an existing Postgres database without a separate search service?
  • How to merge ranked lists from keyword, typo-tolerant and vector search into one result list?

Tools and methods

  • PostgreSQL full-text search — keyword matching with tsvector, websearch_to_tsquery and ts_rank_cd
  • pg_trgm — trigram similarity for typos in names and abbreviations
  • pgvector — semantic search over embeddings
  • Reciprocal Rank Fusion — merges the ranked lists into one
  • Cross-encoder reranking — optional second stage, for example Cohere Rerank
  • BM25 extensions (pg_search, pg_bestmatch.rs) — corpus-aware lexical ranking when built-in ranking falls short
Continue reading — it's free

Google or a one-time email code. No password.