PostgreSQL raises error 22025 (invalid_escape_sequence) when a backslash-escape inside a string literal or bytea value is not recognized.
invalid_escape_sequence (PostgreSQL 22025) appears when an unrecognized backslash escape like "\y" is found in a string. Use E'' strings or double the backslash ("\\") to fix the query.
invalid_escape_sequence
PostgreSQL throws SQLSTATE 22025 - invalid_escape_sequence when it encounters a backslash escape it does not understand inside a string literal or a bytea constant.
The parser expects a valid escape such as \n
, \t
, or a valid octal/hex notation.
Any unknown pattern, or a single unmatched backslash, triggers the error before execution.
An illegal backslash appears most often in text imported from external files, copy-pasted JSON, regular-expression patterns, or dynamically built SQL where escapes are not doubled.
Versions 9.1 and later require either standard_conforming_strings=on or the E'' string prefix to interpret backslashes as escapes.
Mixing modes frequently leads to invalid_escape_sequence.
Correct the literal by doubling the backslash ("\\") or switching to an E-prefixed string so PostgreSQL will treat the escape explicitly.
Validate regex and LIKE patterns the same way.
When loading data with COPY, set the FORMAT option or use the ESCAPE clause to ensure backslashes are interpreted exactly as you intend.
INSERT statements containing Windows file paths like 'C:\\temp\\file.txt' must double each backslash or use E'C:\temp\file.txt'.
JSON text that carries "\u" sequences fails if a trailing character is missing.
Sanitise the JSON or cast through jsonb to detect early.
Always enable standard_conforming_strings in session or postgresql.conf so ordinary strings treat backslashes literally, reducing accidental escapes.
Prefer parameterised queries from a driver or Galaxy SQL editor to auto-escape user input. Validate user-supplied patterns with CHECK constraints.
Error 42601 - syntax_error occurs when the malformed string prevents the query from parsing at all.
Double escaping usually resolves both errors.
Error 22021 - character_not_in_repertoire happens when an escape decodes to an invalid code point. Verify encoding settings after fixing escapes.
.
Consult the official documentation section "String Constants" or run a quick test with E'' strings to see which sequences compile.
No. invalid_escape_sequence is evaluated before encoding checks. After you fix the escape, encoding errors may still appear.
Legacy code relying on backslash escapes must switch to E'' strings. Test thoroughly before changing the server default.
Galaxy highlights invalid escapes in real time and suggests the correct doubling or E'' syntax through its AI copilot, reducing runtime failures.