You appear to be using Java and JDBC. Please read the JDBC tutorial, which describes how to use paramaterized queries to safely insert data without risking SQL injection problems.
Please read the prepared statements section of the JDBC tutorial and these simple examples in various languages including Java.
Since you're having issues with backslashes, not just 'single quotes'
, I'd say you're running PostgreSQL 9.0 or older, which default to standard_conforming_strings = off
. In newer versions backslashes are only special if you use the PostgreSQL extension E'escape strings'
. (This is why you always include your PostgreSQL version in questions).
You might also want to examine:
While it is possible to explicitly quote values, doing so is error-prone, slow and inefficient. You should use parameterized queries (prepared statements) to safely insert data.
In future, please include a code snippet that you're having a problem with and details of the language you're using, the PostgreSQL version, etc.
If you really must manually escape strings, you'll need to make sure that standard_conforming_strings
is on and double quotes, eg don''t manually escape text
; or use PostgreSQL-specific E'escape strings where you \'backslash escape\' quotes'
. But really, use prepared statements, it's way easier.