postgresql 带有文字定界符的 Postgres COPY 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26160816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:38:16  来源:igfitidea点击:

Postgres COPY command with literal delimiter

postgresqlcopydelimiter

提问by toanong

I was trying to import a CSV file into a PostgreSQLtable using the COPYcommand. The delimiter of the CSVfile is comma (,). However, there's also a text field with a comma in the value. For example:

我试图使用COPY命令将 CSV 文件导入到PostgreSQL表中。CSV文件的分隔符是逗号 (,)。但是,还有一个值中带有逗号的文本字段。例如:

COPY schema.table from '/folder/foo.csv' delimiter ',' CSV header

Here's the content of the foo.csvfile:

这是foo.csv文件的内容:

Name,Description,Age
John,Male\,Tall,30

How to distinguish between the literal comma and the delimiter?

如何区分文字逗号和分隔符?

Thanks for your help.

谢谢你的帮助。

回答by Clodoaldo Neto

To have the \to be recognized as a escape character it is necessary to use the text format

要将\其识别为转义字符,必须使用文本格式

COPY schema.table from '/folder/foo.csv' delimiter ',' TEXT

But then it is also necessary to delete the first line as the HEADERoption is only valid for the CSVformat.

但随后也有必要删除第一行,因为该HEADER选项仅对CSV格式有效。