PostgreSQL raises error 22019 (invalid_escape_character) when an unrecognized or malformed backslash escape appears in a string literal.
PostgreSQL error 22019 – invalid_escape_character – occurs when a backslash in a string literal forms an invalid escape sequence. Prefix the literal with E, double the backslash, or remove the stray character to resolve the error.
PostgreSQL Error 22019
The error signals that PostgreSQL found a backslash (\) inside a string literal that does not form a valid escape sequence. When standard_conforming_strings
is on (default since v9.1), backslashes are treated as ordinary characters unless the literal is written with the E''
prefix.
The parser aborts on any illegal escape such as '\q'
or a lone trailing backslash.
Because parsing fails before execution, fixing the SQL text is mandatory.
Invalid escape sequences inside regular quoted strings trigger SQLSTATE 22019. Writing 'C:\files\new\'
without doubling the final backslash is a typical mistake.
For legacy code, setting standard_conforming_strings=off
re-enables backslash escapes globally, but it is discouraged because it masks errors and hinders portability.
Use escape string constants: write the literal as E'C:\\files\\new\'
, where every backslash is doubled.
PostgreSQL then interprets \\
as one backslash in the stored value.
Alternatively, keep standard_conforming_strings
enabled and avoid backslashes entirely by using parameterized queries or the $$
dollar-quote syntax when no escapes are required.
Bulk-inserting Windows file paths often fails. Convert them with E
literals or replace backslashes with /
before loading.
Generating JSON using text concatenation can introduce stray backslashes.
Switch to jsonb_build_object
or parameter placeholders to eliminate manual escaping.
Always enable standard_conforming_strings
, rely on parameterized statements, or use E''
literals when escapes are required.
Maintain code reviews that flag unprefixed strings containing backslashes.
Galaxy’s SQL editor highlights invalid escapes in real time and suggests correct E''
syntax, preventing the error before you run the query.
SQLSTATE 42601
- syntax_error: raised by any malformed SQL, including unterminated strings.
SQLSTATE 22025
- invalid_escape_sequence: appears when a valid escape flag like \u
is followed by bad characters in CONVERT
or LIKE
patterns.
.
No. The error protects you from unintended characters reaching the database. It surfaces invalid escape usage so you can correct the SQL.
You can set standard_conforming_strings=off
cluster-wide, but it is unsafe and discouraged. Fix the literals instead.
Older versions defaulted to non-standard backslash escapes. Newer releases enforce standard strings, exposing hidden issues.
Galaxy’s real-time linter flags unescaped backslashes and suggests using E''
strings or parameters before the query runs.