PostgreSQL Error 22025 Invalid Escape Sequence Fix
In this article
PostgreSQL error 22025 occurs because the database encounters a backslash (\) in a string literal that it cannot recognize as a valid escape sequence. To fix this, you must either prefix the string with an 'E' (e.g., E'string\n') to enable escape string constants or use dollar quoting (e.g., $$string\n$$) to treat the backslash as a literal character.
What You'll Need
- Access to a PostgreSQL query tool (such as psql, pgAdmin, or a database IDE).
- The SQL statement that is currently triggering the error.
- Administrative or write permissions for the database if you are updating existing data.
Step-by-Step
- Identify the offending string: Locate the specific line in your SQL query where the error occurs. Look for backslashes used in file paths (e.g., 'C:\Users\Name'), Windows registry keys, or LaTeX-style formatting.
- Choose your resolution method: Depending on your needs, select one of the following three methods to handle the backslash.
- Option A: Use Escape String Syntax (The 'E' Prefix):
If you actually want the backslash to perform a function (like
\nfor a new line or\tfor a tab), add a capital 'E' immediately before the opening single quote of the string.Example: Change
'Line one\nLine two'toE'Line one\nLine two'. - Option B: Use Dollar Quoting (Best for Paths):
If the backslash is just part of the text (like a folder path) and you do not want PostgreSQL to interpret it as a special command, wrap the string in double dollar signs instead of single quotes.
Example: Change
'C:\Program Files\App'to$$C:\Program Files\App$$. - Option C: Double the Backslashes:
You can escape the backslash itself by adding a second backslash. This tells PostgreSQL that the first backslash is meant to be a literal character.
Example: Change
'C:\Program Files\App'to'C:\\Program Files\\App'. - Execute the updated query: Run the corrected SQL statement in your environment to verify the error is resolved.
Common Mistakes
- Using lowercase 'e': The escape prefix must be a capital 'E'. Using
e'string'will result in a syntax error. - Mixing quotes and dollar signs: Do not start a string with a single quote and end it with dollar signs. You must use one consistent quoting method for the entire string.
- Over-escaping: When using dollar quoting (
$$), do not double the backslashes. Dollar quoting treats everything inside the signs literally; doubling the backslashes will result in two backslashes being stored in the database. - Assuming standard_conforming_strings is off: Many users assume their database is configured to treat backslashes as literals by default. In modern PostgreSQL versions,
standard_conforming_stringsisonby default, which is why the 'E' prefix or dollar quoting is required for escape sequences.
Tips
- Prefer Dollar Quoting for Long Text: If you are inserting large blocks of text, HTML, or regex patterns containing many backslashes and single quotes, dollar quoting is the cleanest method and prevents "quote fatigue."
- Custom Dollar Tags: If your text contains
$$, you can put a tag between the dollar signs to create a unique delimiter, such as$body$Your text here$body$. - Check Application Logic: If this error is coming from an application (like a Python or Java app), ensure your database driver is handling parameter binding correctly. Using parameterized queries (prepared statements) usually avoids this error entirely because the driver handles the escaping.
Troubleshooting
If you have applied the fixes above and the error persists, try these steps:
- Check for hidden characters: Ensure there are no invisible control characters or non-breaking spaces around your backslashes that might be confusing the parser.
- Verify the standard_conforming_strings setting: Run
SHOW standard_conforming_strings;. If it isoff, the database treats backslashes as escape characters by default. If it ison, it treats them as literals unless the 'E' prefix is used. Changing this setting globally is not recommended as it can break existing queries. - Test with a simple string: Try running a basic
SELECT E'\n';. If this fails, there may be a deeper issue with your database version or client tool. - Update Client Tools: If you are using an extremely old version of pgAdmin or psql, update the tool to ensure it is sending the SQL commands using the correct encoding and protocol.
Frequently Asked Questions
Why did my queries work in an older version of PostgreSQL?
Older versions of PostgreSQL had different default settings for standard_conforming_strings. In the past, backslashes were often treated as escape characters by default, whereas modern versions follow the SQL standard more strictly.
Does dollar quoting affect performance?
No, dollar quoting is a syntactic convenience for the parser and has no measurable impact on the performance of the query or the storage of the data.
Can I fix this globally so I don't have to use 'E' or $$?
You can change the standard_conforming_strings parameter in the postgresql.conf file, but this is highly discouraged. It can cause existing data to be misinterpreted and may lead to security vulnerabilities like SQL injection if not handled carefully.
Which method is the most secure?
Using parameterized queries (prepared statements) via your programming language's database library is the most secure method. It separates the SQL command from the data, eliminating the risk of escape sequence errors and SQL injection.
Does this error happen with forward slashes?
No. Forward slashes (/) are not escape characters in PostgreSQL and do not trigger error 22025.