PostgreSQL Error 22019 Invalid Escape Character Fix Guide
In this article
PostgreSQL error 22019 occurs when you use an invalid character as an escape character in a LIKE or SIMILAR TO pattern. This happens because PostgreSQL requires escape characters to be single-byte and valid within the current encoding—typically a letter, digit, or underscore. Using symbols like @, #, or spaces as escapes triggers this error. The fix is simple: replace the invalid escape character with a valid one or remove the ESCAPE clause entirely if not needed.
What You'll Need
- Access to a PostgreSQL database (version 9.0 or later)
- Permissions to run SELECT, UPDATE, or other SQL queries involving LIKE or SIMILAR TO
- A text editor or SQL client (such as psql, pgAdmin, DBeaver, or DataGrip)
- Knowledge of the exact query causing the error
Step-by-Step
- Identify the exact SQL query that triggered the error. Look for the word ESCAPE followed by a single character in a LIKE or SIMILAR TO clause. Example:
WHERE name LIKE '%\_%' ESCAPE '@'. - Check the character specified after ESCAPE. Valid escape characters in PostgreSQL are single-byte characters that are not NULL, and are not control characters. Letters (a-z, A-Z), digits (0-9), and underscore (_) are acceptable. Symbols like @, #, $, %, ^, &, *, (, ), -, +, =, {, }, [, ], |, \, :, ;, &,lt;, &,gt;, ?, /, ., ,, !, ~, `, ', ", or space are invalid.
- If the escape character is invalid, replace it with a valid one. For example, change ESCAPE '@' to ESCAPE '\'. The backslash (\) is the most common and recommended escape character in PostgreSQL.
- If you don’t actually need escaping—for example, if your pattern doesn’t contain wildcard characters like % or _—remove the ESCAPE clause entirely. Example: Change
WHERE name LIKE '%test%' ESCAPE '@'toWHERE name LIKE '%test%'. - If you're using SIMILAR TO, the same rules apply. Replace or remove the invalid escape character. Example: Change
WHERE text SIMILAR TO '%#_%' ESCAPE '#'toWHERE text SIMILAR TO '%\\_%' ESCAPE '\'. - Test the corrected query in your SQL client. Run it on a small dataset first to confirm it works without error.
- Apply the fix to all affected queries in your application code, stored procedures, or views. Search your codebase for ESCAPE followed by non-alphanumeric characters to find all instances.
- Restart your application or database connection if needed, though this is rarely required—just re-running the query after correction is sufficient.
Common Mistakes
- Using the percent sign (%) as an escape character—this is invalid because % is a wildcard in LIKE patterns and cannot be used to escape itself.
- Assuming any symbol works as an escape character because it works in other databases like MySQL or SQL Server. PostgreSQL is stricter about escape character validity.
- Using a multi-byte character (e.g., UTF-8 emoji or accented letters) as an escape character. PostgreSQL only allows single-byte characters for ESCAPE clauses.
- Accidentally typing two backslashes when you meant one: ESCAPE '\\' is wrong; use ESCAPE '\'. The double backslash is interpreted as a literal backslash, not an escape declaration.
- Leaving an ESCAPE clause in place when the pattern contains no wildcards. This adds unnecessary complexity and increases risk of error.
Tips
- Always use backslash (\) as the escape character unless you have a strong reason not to—it's the PostgreSQL standard and universally supported.
- If you need to match a literal percent sign (%) or underscore (_) in a LIKE pattern, use ESCAPE '\', then write \% or \_.
- Use the SIMILAR TO operator only when you need regex-like patterns; otherwise, stick with LIKE for simplicity and performance.
- When building dynamic SQL in application code (e.g., Python, Java, Node.js), validate or sanitize escape characters before injecting them into queries to prevent this error.
- Use parameterized queries whenever possible. They avoid manual escaping entirely and eliminate this class of error.
- Enable PostgreSQL’s log_statement setting to log all queries. This helps you catch problematic ESCAPE clauses in production logs before they cause user-facing errors.
Troubleshooting
If the error persists after changing the escape character:
- Verify there are no hidden characters in your query. Copy the query into a plain text editor (like Notepad++ or VS Code) and enable display of invisible characters. Look for non-breaking spaces, zero-width spaces, or Unicode control characters around the ESCAPE clause.
- Check your database encoding. Run
SHOW client_encoding;. If it’s set to something unusual like SJIS or BIG5, ensure your escape character is valid in that encoding. Most common encodings (UTF8, LATIN1) support standard ASCII escape characters. - Test the query directly in psql. Sometimes application-level drivers or ORMs interfere with query parsing. If it works in psql but not in your app, the issue is in how the app constructs the query.
- Look for nested ESCAPE clauses. Some tools auto-generate queries with multiple ESCAPE clauses. Example:
WHERE col LIKE '%\_%' ESCAPE '\' ESCAPE '@'. Remove the duplicate or invalid one. - Update PostgreSQL if you’re on a very old version (pre-9.0). Earlier versions had inconsistent escape handling.
- If you’re using an ORM like Django, Hibernate, or Entity Framework, check its documentation for how it handles LIKE escaping. Some ORMs auto-escape % and _ and may insert invalid ESCAPE clauses. Disable auto-escaping if it’s causing conflicts.
Frequently Asked Questions
Can I use a space as an escape character in PostgreSQL?
No. Spaces are not valid escape characters. PostgreSQL only allows single-byte alphanumeric characters or underscore. Using a space will always trigger error 22019.
Why does PostgreSQL reject @ as an escape character but MySQL accepts it?
PostgreSQL enforces stricter SQL standard compliance for escape characters. While MySQL allows any character as an escape, PostgreSQL restricts it to prevent ambiguity and encoding issues, especially in multi-byte UTF-8 environments.
Do I need to escape % and _ in SIMILAR TO patterns?
Yes. In SIMILAR TO, % and _ are wildcards just like in LIKE. To match them literally, use ESCAPE '\' and write \% or \_.
Can I disable escape character checking in PostgreSQL?
No. There is no configuration setting to bypass this validation. It’s a core part of SQL standard compliance and cannot be turned off.
What if my data contains actual backslashes and I need to escape them?
Use double backslashes: to match a literal backslash, write \\ in your LIKE pattern with ESCAPE '\'. For example: WHERE path LIKE '%\\%' ESCAPE '\' matches any path containing a backslash.
Is this error only triggered by LIKE and SIMILAR TO?
Yes. Error 22019 is specific to the ESCAPE clause in LIKE and SIMILAR TO patterns. It does not occur with regular expressions (using ~ or ~* operators), which use different syntax.