PostgreSQL error 22019 occurs when an invalid escape character is used in a LIKE pattern or string literal. This typically happens when the escape character in a LIKE ESCAPE clause is longer than one character or when backslash escapes are used incorrectly.
The "invalid_escape_character" error (SQLSTATE 22019) is a data exception error that occurs when PostgreSQL encounters an invalid escape character in pattern matching or string operations. Escape characters in PostgreSQL serve specific purposes: they allow special characters to be included literally in LIKE patterns or to introduce escape sequences in strings. However, escape characters must follow strict syntax rules. This error typically happens when: 1. The ESCAPE clause in a LIKE expression specifies a character that is longer than one byte 2. The escape character in a LIKE pattern is invalid or improperly specified 3. Backslash escapes are used in regular strings without the E prefix (when standard_conforming_strings is on) 4. Invalid escape sequences appear in string literals
The ESCAPE clause must specify exactly one character. Check your LIKE patterns:
Wrong (multi-character escape):
SELECT * FROM table WHERE column LIKE '%test%' ESCAPE 'ab';
Wrong (empty escape):
SELECT * FROM table WHERE column LIKE '%test%' ESCAPE '';
Correct (single character):
SELECT * FROM table WHERE column LIKE '%test%' ESCAPE '\\';
Choose a single character that does not appear in your pattern:
Using backslash:
SELECT * FROM table WHERE column LIKE '%\\_%' ESCAPE '\\'; -- Find underscore
Using exclamation mark:
SELECT * FROM table WHERE column LIKE '%!%%' ESCAPE '!'; -- Find percent sign
Using pipe:
SELECT * FROM table WHERE column LIKE '%|_%' ESCAPE '|';
Common choices: \\, !, |, #, ^ (any ASCII character not in your search pattern)
When using backslash escapes in strings, use E prefix or double the backslash:
Wrong (with standard_conforming_strings = on):
SELECT 'line1\\\\nline2';
Correct (E prefix):
SELECT E'line1\\\\nline2';
Alternative (doubled backslash):
SELECT 'line1\\\\\\\\nline2';
Verify the current setting:
SHOW standard_conforming_strings;
If 'on' (default in PostgreSQL 9.1+), all escape sequences in regular strings require E prefix.
To verify and show setting:
SHOW standard_conforming_strings;
SHOW escape_string_warning;
For troubleshooting:
SET standard_conforming_strings = on; -- Enforce strict escaping
SET escape_string_warning = on; -- Warn about backslashes
Often the simplest fix is to avoid escapes. Only use ESCAPE when you need to search for % or _:
Instead of:
SELECT * FROM table WHERE column LIKE '%\\\\%' ESCAPE '\\\\';
Use simpler alternative:
SELECT * FROM table WHERE column LIKE '%\\\\%';
Or just check for literal text:
SELECT * FROM table WHERE column LIKE '%percent%';
Use parameterized queries or prepared statements to avoid escaping issues:
PostgreSQL with parameterized query:
PREPARE stmt AS
SELECT * FROM table WHERE column LIKE $1 ESCAPE $2;
EXECUTE stmt('%test%', '!');
Using application drivers (e.g., Node.js):
const query = 'SELECT * FROM table WHERE column LIKE $1 ESCAPE $2';
const result = await db.query(query, ['%test%', '!']);
This lets the driver handle escaping correctly.
Test your fixes:
Test basic LIKE with single-char escape:
SELECT * FROM table WHERE column LIKE 'a!%b' ESCAPE '!';
Test finding special characters:
SELECT * FROM table WHERE column LIKE '%!_%' ESCAPE '!';
Test string escapes:
SELECT E'Test\\\\nMultiline';
Enable warnings to catch issues:
SET escape_string_warning = on;
PostgreSQL Escape Rules:
1. ESCAPE in LIKE: Must be exactly one character. Common choices: backslash (\\), exclamation (!), pipe (|), hash (#), caret (^).
2. Backslash Escapes: Require E prefix in standard-conforming mode (PostgreSQL 9.1+). Valid sequences: \\\\b (backspace), \\\\f (form feed), \\\\n (newline), \\\\r (carriage return), \\\\t (tab), \\\\xHH (hex), \\\\oOOO (octal).
3. Alternative Quoting: Use dollar-quoting to avoid escaping: $$string$$ or $tag$string$tag$.
4. Unicode Escapes: Use U& prefix for Unicode: U&'d\\\\0061ta' (requires UESCAPE for custom escape char).
5. Historical Note: PostgreSQL 8.1 and earlier had standard_conforming_strings = off by default, allowing backslash escapes in regular strings.
6. Migration: Audit old SQL when upgrading PostgreSQL versions. Use escape_string_warning to identify problematic queries.
7. Best Practice: Use parameterized queries or prepared statements. Drivers handle escaping automatically, avoiding these errors.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL