Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL csv import from a MySQL csv export?

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: ...
like image 993
FirstTimePoster Avatar asked Sep 20 '11 11:09

FirstTimePoster


1 Answers

  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 '\t' NULL AS '\\N' ESCAPE E'\\' CSV
like image 200
FirstTimePoster Avatar answered Oct 07 '22 03:10

FirstTimePoster