patternsqlMinor
Escaping delimiter in postgresql
Viewed 0 times
postgresqldelimiterescaping
Problem
I am trying to import data into postgres. Data fields are comma separated, strings may contain commas, in which case those are escaped:
Data structure:
Data file:
Import command (run under Linux from bash)
Error:
I've been fighting and google against this problem for quite a few hours and do not understand what I am doing wrong?
Data structure:
create table v (a varchar);Data file:
bus
'Gat\,\\e\'way_MQB'Import command (run under Linux from bash)
cat data.csv | psql -d database -c "copy v (a) from stdin with delimiter ',' escape '\\' CSV header"Error:
ERROR: extra data after last expected columnI've been fighting and google against this problem for quite a few hours and do not understand what I am doing wrong?
Solution
Try:
You don't need to escape the escape character when specifying it. QUOTE defaults to double-quote so you need to pass that.
cat data.csv | psql -d database -c "copy v (a) from stdin with delimiter ',' escape '\' quote '''' CSV header"You don't need to escape the escape character when specifying it. QUOTE defaults to double-quote so you need to pass that.
Code Snippets
cat data.csv | psql -d database -c "copy v (a) from stdin with delimiter ',' escape '\' quote '''' CSV header"Context
StackExchange Database Administrators Q#76812, answer score: 6
Revisions (0)
No revisions yet.