使用\ copy命令将CSV导入Postgres时如何转义\ n(换行符)?

时间:2016-11-18 13:31:01

标签: sql database postgresql csv escaping

我正在尝试使用CSV文件中的\ copy将数据导入到我的postgres表中 数据如下:

1,2,"First Line \n Second Line", "Some other data"

我的动机是在导入数据时保留'\ n'。

以下作品:

insert into table (col1, col2, col3, col4) values (1, 2, E'First Line \n Second Line', 'Some other data')

但是如何使用\ copy命令获得相同的结果?

2 个答案:

答案 0 :(得分:4)

你唯一可以在CSV中转义的是字段分隔符(默认情况下为"),你可以通过加倍来逃避它。

换行符无法转义,实际上没有必要,因为您可以使用文字换行符。

这是CSV中的一行:

1,2,"First Line
Second Line", "Some other data"

答案 1 :(得分:1)

我担心您必须将csv修改为要保留的反斜杠\n

b=# copy t from stdout;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> "First Line \n Second Line"
>> "First Line \\n Second Line"
>> \.
COPY 2
b=# select * from t;
              a
-----------------------------
 "First Line                +
  Second Line"
 "First Line \n Second Line"
(2 rows)
相关问题