DEV Community

Cover image for How to query with PostgreSQL wildcards like a Pro

How to query with PostgreSQL wildcards like a Pro

Kevin Peters on October 14, 2019

Originally published on my blog: https://www.kevinpeters.net/how-to-query-with-postgre-sql-wildcards-like-a-pro/ How to filter with any...
Collapse
 
dmfay profile image
Dian Fay • Edited

Since you're running on Postgres, you can use ILIKE for a case-insensitive LIKE instead of multiple conditions! There are also a couple different flavors of regular expression which are even more flexible.

The leading wildcard makes your "full-text search" non-sargable and therefore likely to perform poorly on larger tables or text fields. For actual full-text search you'll want to use tsvectors and to_tsquery().

Collapse
 
igeligel profile image
Kevin Peters

Thanks, knew about ILIKE but this is just an extension for PostgreSQL. Regular expressions are of course more flexible. Also thanks for mentioning the full-text search. I will extend the article to include these points.

Collapse
 
256hz profile image
Abe Dolinger

Funny, I was just thinking about naming my next child Melloremipsum.

Collapse
 
zchtodd profile image
zchtodd

Great article! I always forget that you can use underscore in a LIKE.