patternsqlMinor
Postgres copy data with \xYY as plain string instead of interpreting as encoded string
Viewed 0 times
postgreswithinterpretingencodedxyyinsteaddatastringplaincopy
Problem
I have a log file full of URLs, generated by Bro IDS.
When Bro logs a URL with non-ascii characters in it, it inserts \xYY where YY is the hexadecimal character code. Also, some URLs contain "\x".
Is there a setting or flag I can use with the
When Bro logs a URL with non-ascii characters in it, it inserts \xYY where YY is the hexadecimal character code. Also, some URLs contain "\x".
Is there a setting or flag I can use with the
COPY or \copy command to stop postgres from trying to interpret these string sequences and just put them in the text field as is?Solution
The backslash
backslash sequences are recognized by
...
\xdigits | Backslash x followed by one or two hex digits specifies the character with that numeric code
.. which seems to be just as intended.
To avoid this effect you could use the CSV format instead, where the backslash has no special meaning:
Be sure to read the manual to learn about additional differences.
Alternatively you could replace every
If the above doesn't solve your problem, please supply:
\ is the default escape character in the (default) text format of COPY. The format you describe is recognized as (quoting the manual here) backslash sequences are recognized by
COPY FROM...
\xdigits | Backslash x followed by one or two hex digits specifies the character with that numeric code
.. which seems to be just as intended.
To avoid this effect you could use the CSV format instead, where the backslash has no special meaning:
COPY tbl FROM '/path/to/file/log.sql' (FORMAT csv);Be sure to read the manual to learn about additional differences.
Alternatively you could replace every
\ with \\ in your file with a sed script or something.If the above doesn't solve your problem, please supply:
- PostgreSQL version
- Database encoding
- Demo data
- Definition of the table you
COPYto
- The exact
COPYinvocation you used
Code Snippets
COPY tbl FROM '/path/to/file/log.sql' (FORMAT csv);Context
StackExchange Database Administrators Q#36444, answer score: 4
Revisions (0)
No revisions yet.