A simple 'fuzzy' search using PostgreSQL and Kysely
How to perform a simple, robust search using only basic PostgreSQL features
Chris Kahlefendt
Jun 6, 2025
Introduction
If you build web applications, you'll inevitably need to add search functionality. Whether it's for employees
, products
, or orders
, your users expect to find what they're looking for quickly.
The immediate thought might be to set up a dedicated search engine like Elasticsearch, but for many projects, that's overkill.
It adds another service to maintain for a feature that doesn't need to be that complex.
So, what's the alternative? If you are already using PostgreSQL, you're in luck. In the sense of "Just use Postgres", we can simply rely on PostgreSQL's built-in capabilities to perform searches.
In this post, I want to explore how to use Postgres to build an effective search functionality using built-in pattern matching and trigram capabilities.
I work a alot with TypeScript and Node.js and I've grown to love the type-safe query builder Kysely. While I was looking for a solution, my goal was to find one that is fully type-safe and easily implementable with Kysely. As I would have loved to find a post that explains how to do a simple search with Postgres and also how to implement it with Kysely, I will show both side by side in this article.
And to make it a bit more interactive, we will run all SQL queries directly in the browser using PGLite.