6

Will it work?

MySQL export:

SELECT * INTO OUTFILE 'C:/data.csv'
FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM table;

PostgreSQL:

COPY table FROM 'C:/data.csv' WITH DELIMITER AS '\t' NULL AS '\\N' CSV

There is missing columns for some reason. So I believe there is problem in delimiter. Am I correct, what can I do? I can inspect row with cause error below. But which characters I must look for?

ERROR:  missing data for column "Column21"
CONTEXT:  COPY table, line 88219: ...
1
  • So at 88219 line contain sting with escaped quote: " ... \" ... " Seems like postgres fail to unquote this. Is there special option for this? Commented Sep 20, 2011 at 12:06

2 Answers 2

16
  1. As mentioned by @knitti postgres need to know escape character: ESCAPE '\'
  2. OPTIONALLY ENCLOSED BY '"' is bad format for csv. It's better to force quoting.

Full code:

mysql 5.5.15:

SELECT *
INTO OUTFILE 'C:/data.csv'
FIELDS TERMINATED BY '\t' ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM table;

postgres 9.0.3:

COPY table FROM 'C:/data.csv' WITH DELIMITER AS E'\t' NULL AS '\\N' ESCAPE E'\\' CSV
Sign up to request clarification or add additional context in comments.

2 Comments

Just what I was looking for my friend!
I needed to escape the delimiter as E'\t' for the above to work
0

Default quote character in PostgreSQL is double-quote, but the documentation says, you can use any single one-byte-character. Try adding

ESCAPE '\\'

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.