PostgreSQL Error 2200C: Invalid Use of Escape Character
In this article
PostgreSQL error 2200C (invalid_escape_sequence) means a string literal contains a backslash escape that PostgreSQL does not recognise. The fix is to either double the backslash (write \\ to represent one literal backslash) or switch to a dollar-quoted string such as $tag$...$tag$. The error appears when standard_conforming_strings is on (the default) and you write something like 'C:\Users' or '\d' inside a normal quoted string, because PostgreSQL treats the backslash as an escape introducer only in E'...' strings, and in ordinary strings a backslash followed by an unrecognised character is rejected.
What You'll Need
- Access to a PostgreSQL client (psql, pgAdmin, DBeaver, or your application's database console) and permission to run the failing query.
- The exact query or statement that triggered the error, including the string literal that contains the backslash.
- Knowledge of the PostgreSQL version you are connecting to, because string handling and
standard_conforming_stringsdefaults can differ between older and current releases.
Step-by-Step
- Locate the failing literal. Read the error message. It usually names the position or shows the offending token. In psql the message reads something like
ERROR: invalid escape sequence at or near "\U"orinvalid escape sequence at end of string. Find the string literal in your SQL that contains a backslash. - Check whether the string is an E-string. In PostgreSQL, only strings prefixed with
E(for exampleE'C:\\temp') process backslash escapes. Ordinary strings such as'C:\temp'are literal whenstandard_conforming_stringsis on. If your literal has noEprefix, the backslash is being read literally and the error is coming from somewhere else, often a function argument or a nested quote. - Double the backslash in ordinary strings. If you need a literal backslash inside a normal quoted string, write two backslashes. For example,
'C:\\Users\\me'produces the textC:\Users\me. This is the most common fix for path strings and regular expressions passed as plain literals. - Use dollar quoting for complex text. For strings containing many backslashes, JSON, regex, or Windows paths, wrap the value in dollar quotes:
$body$C:\Users\me\file.txt$body$. Everything between the tags is taken literally, so no escaping is needed. Choose a tag that does not appear in the text. - Use E-strings only when you want escapes. If you deliberately want a newline, tab, or Unicode escape, write
E'line1\nline2'. Inside an E-string, a backslash must be followed by a valid escape character (\n,\t,\\,\',\x41, and so on). An unrecognised sequence such as\dor\Uraises error 2200C. - Test the corrected statement. Run the query in psql or your client. If it succeeds, update the application code, migration file, or stored procedure that contained the original literal. Search the codebase for the same pattern so you fix every occurrence, not just the one that failed.
- Check the server setting if behaviour is unexpected. Run
SHOW standard_conforming_strings;. If it returnsoff, ordinary strings treat backslashes as escapes and the rules change. Most current installations default toon, but a legacy setting or a connection option can override it. Do not change this setting casually on a shared database; fix the literals instead.
Common Mistakes
- Adding an
Eprefix to fix the error. That makes the string an escape string and can introduce new invalid sequences. The safer fix is doubling the backslash or using dollar quoting. - Doubling backslashes inside a dollar-quoted string. Dollar quoting is literal, so
$q$a\\b$q$stores two backslashes, not one. - Confusing the SQL literal with the value stored.
'\\n'in an ordinary string is a backslash followed byn, not a newline;E'\n'is a newline. - Forgetting that the same escaping rules apply inside function bodies and dynamic SQL built with
EXECUTEorformat(). A query that works when typed directly can fail when assembled as a string. - Assuming the error is about the data. Error 2200C is raised while parsing the SQL text, before any row is read, so the table contents are not the cause.
Tips
- Prefer dollar quoting for JSON, regex, and file paths. It removes the need to count backslashes and makes the intent obvious to the next reader.
- When passing values from an application, use parameterised queries (
$1,$2) rather than embedding literals. The driver handles escaping, which avoids this class of error entirely. - Use
SELECT 'your literal'::text;as a quick test to see exactly what PostgreSQL stores before you put the value into a larger statement. - Keep
standard_conforming_stringsat its default and fix the SQL instead of changing server behaviour, because other applications may rely on the current setting.
Troubleshooting
If the error persists after doubling the backslash, check whether the literal is nested inside another string. Dynamic SQL built with EXECUTE 'SELECT ''C:\\temp''' needs two levels of quoting, and each level doubles the backslashes again. Simplify by using format() with %L or by using dollar quoting for the outer string.
If the error mentions an escape sequence you cannot find, search the statement for \ characters, including inside comments and quoted identifiers. Some clients also interpret backslashes before sending the query, so test the same statement directly in psql to see whether the client is altering it.
If the error appears only in a migration or a stored procedure, inspect the function body. Function bodies are often written as dollar-quoted strings, but if they are written as ordinary strings, backslashes inside them must be doubled. Recreate the function with a dollar-quoted body to avoid the problem.
If you are on an older PostgreSQL release and the behaviour differs from documentation, confirm the server version with SELECT version(); and check the string syntax section of the manual for that release. Do not rely on memory of another version's defaults.
Frequently Asked Questions
What does PostgreSQL error 2200C mean?
It means a string literal contains a backslash escape sequence that PostgreSQL does not recognise. The SQLSTATE code is 2200C and the condition name is invalid_escape_sequence.
Why does 'C:\Users' fail but 'C:\\Users' work?
In an ordinary string, a single backslash before an unrecognised character is rejected. Two backslashes represent one literal backslash, so the second form stores C:\Users correctly.
Should I use E-strings to fix this error?
Only if you actually want escape processing. Adding E turns on escape interpretation and can create new invalid sequences. Doubling the backslash or using dollar quoting is usually safer.
Does standard_conforming_strings affect this error?
Yes. When it is on, ordinary strings treat backslashes literally and reject invalid escape sequences. When it is off, backslashes are escapes in all strings. Check the setting with SHOW standard_conforming_strings;.
Can parameterised queries prevent error 2200C?
Yes. Passing values as parameters lets the driver handle quoting and escaping, so you do not embed backslashes in SQL text at all.
Does dollar quoting work for any string?
Yes, as long as the chosen tag does not appear in the text. Use a distinctive tag such as $body$ or $json$ to avoid accidental termination.