SQLSTATE 22025 ERROR Class 22: Data Exception

invalid_escape_sequence LIKE pattern must not end with escape character — 22025

PostgreSQL error "LIKE pattern must not end with escape character" (SQLSTATE 22025): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A LIKE/ILIKE pattern ends with the escape character (default backslash) with nothing to escape. PostgreSQL raises SQLSTATE 22025 (invalid_escape_sequence).

  • Usually from a trailing backslash in a user-supplied search term.
  • The escape char must always precede a character to escape.
  • Common when wildcards are built from raw input.

What the server log shows

ERROR:  LIKE pattern must not end with escape character

Why PostgreSQL raises this — what the manual says

Section 9.7.1 LIKE:

“To match the escape character itself, write two escape characters.”

In LIKE, the escape character makes the next character literal. If the pattern ends with the escape character there is nothing to escape, which is invalid and reported as 22025.

Common causes

  • A trailing backslash in the search pattern.
  • Concatenating user input that ends with the escape character.
  • Double-escaping gone wrong in application code.

How to fix it

  1. Escape user input properly before building the LIKE pattern (double the backslash, or escape %/_).
  2. Choose a different escape character: LIKE pattern ESCAPE '!'.
  3. Disable escaping when not needed: LIKE pattern ESCAPE ''.

Related & next steps

Reference: PostgreSQL 18 Section 9.7 “Pattern Matching”.

Was this helpful?