9

Let's say my table is:

  id    text
+-----+----------+
  123 | foo bar
  321 | bar "baz"

Is there any way to escape those quotes around 'baz' when dumping?

My query is in the form:

SELECT text FROM aTable WHERE ...

And I would like the output to be:

foo bar
bar \"baz\"

rather than:

foo bar
bar baz
3
  • Have you tried the ESCAPE clause from here? Commented Apr 26, 2011 at 2:17
  • @dawebber I was trying to avoid COPY since I'm SELECT'ing from a JOIN, and there's no way to use copy on that, right? Commented Apr 26, 2011 at 16:02
  • yes you can. Use COPY (SELECT * FROM table) TO ... . It was on the same page I quoted earlier, midway through the page Commented Apr 26, 2011 at 16:26

2 Answers 2

10

You probably want to use replace:

SELECT REPLACE(text, '"', E'\\"') FROM aTable WHERE ...

You'll need to escape your escape character to get a literal backslash (hence the doubled backslash) and use the "E" prefix on the replacement string to get the right escape syntax.

UPDATE: And thanks to a_horse_with_no_name's usual strictness (a good thing BTW), we have a solution that doesn't need the extra backslash or non-standard "E" prefix:

set standard_conforming_strings = on;
SELECT REPLACE(text, '"', '\"') FROM aTable WHERE ...

The standard_conforming_strings option tells PostgreSQL to use standard syntax for SQL strings:

This controls whether ordinary string literals ('...') treat backslashes literally, as specified in the SQL standard.

This would also impact your \x5C escape:

If the configuration parameter standard_conforming_strings is off, then PostgreSQL recognizes backslash escapes in both regular and escape string constants. This is for backward compatibility with the historical behavior, where backslash escapes were always recognized.

Sign up to request clarification or add additional context in comments.

9 Comments

Using the backslash that way is deprecated. It is highly recommended to use standard_conforming_strings = on which will be the default in the next release if I'm not mistaken. postgresql.org/docs/9.0/static/…
@a_horse_with_no_name: What's the best way to get a ` in a string that works in PostgreSQL 8 and 9 without producing any warnings and doesn't care about the standard_conforming_strings` setting? A lot of people (including me) have to deal with both 8 and 9 and many (such as Heroku users) don't have full control over the PostgreSQL configuration.
@mu is too short: getting a single quote ' into a string is easy: simply put two quotes into the string: 'Peter''s bike'. That is unaffacted by the standard_conforming_strings. The same is true for a double quote: '12" Inch' standard_conforming_strings is only about using a backslash in the literal
Thanks guys, that's exactly what I was looking for. By the way would it be ok to do the following to avoid the double backslash: REPLACE(text, '"', E'\x5C"')?
@a_horse_with_no_name: No, I'm talking about the "\" not the quotes (I forgot to close my backticks in the comment and it is too late to fix it); that single backtick is supposed to be `\`. Sigh, looks like I'm having backslash problems in this comment now, how appropriate.
|
2

You can use the following incarnation of the COPY command:

COPY (SELECT * FROM table) TO ... WITH FORMAT 'CSV', ESCAPE '<WHATEVER ESCAPE CHARACTER YOU WANT>'

as described here.

You might not have to do anything, as in some cases your QUOTE option will be doubled automatically. Please consult examples for the referenced link. You can also use VALUES in addition to SELECT. No further data mangling should be necessary.

This is assuming you are using 7.3 or higher. The syntax is slightly different between 7.3 and 9.0, so please consult the appropriate docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.