从MySQL csv导出导入PostgreSQL csv?

时间:2011-09-20 11:33:26

标签: mysql postgresql csv

会起作用吗?

MySQL导出:

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

出于某种原因缺少列。所以我相信分隔符存在问题。我纠正了,我该怎么办?我可以检查以下原因错误的行。但是我必须找哪些角色?

ERROR:  missing data for column "Column21"
CONTEXT:  COPY table, line 88219: ...

2 个答案:

答案 0 :(得分:13)

  1. 如@knitti所述,postgres需要知道转义字符:ESCAPE'\'
  2. 选择性地封装'''是csv的不良格式。最好强行引用。
  3. 完整代码:

    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
    

答案 1 :(得分:0)

PostgreSQL中的默认引号字符是双引号,但the documentation表示,您可以使用任何单个单字节字符。尝试添加

ESCAPE '\\'