I want to escape backslash by \b so I can later on process it normally. So far I use a command that return tab separated result which coverts nulls into \N as well (for mysql load into):

PGPASSWORD=$PASS psql -qtAX -U $USER -h $HOST -p $PORT -d $DB -AF $'\t' -P 'null=\N'

Any ideas?

1 Answer 1


If I'm understanding your question...

psql offers options for formatting its output (e.g., how it delimits columns and rows, how it presents null values, whether it includes a footer, whether it presents tuples only, whether it presents using its expanded format, etc.); however, I don't believe it offers options to encode, escape, or otherwise process row/field-level data, such as replacing backslashes in stored data.

To accomplish what you seem to be trying to accomplish, I believe you'd need to use a SQL function like replace or regexp_replace, or would need to post-process your output (e.g., using sed).

Several loosely related thoughts:

  • You might want to consider an alternative to \b, as \b often denotes a backspace in backslash encodings.
  • You might want to consider an alternative to PGPASSWORD (and PASS) use, as it may be exposing your password to other users, known and otherwise (as environment variables can be extracted).
  • PGHOST, PGPORT, PGUSER, etc. may be worth checking out as possible alternatives to specifying them for psql (not a terribly important thought, though possible interesting to you).

P.S. Thanks for posting your question... Lots of great psql options there.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.