postgresql 错误:在 psql 中使用 \copy 时缺少列的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26747748/
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-09-09 05:49:33  来源:igfitidea点击:

ERROR: missing data for column when using \copy in psql

postgresqlpsqlpostgresql-copy

提问by nathanmgroom

I'm trying to import a .txt file into PostgreSQL. The txt file has 6 columns:

我正在尝试将 .txt 文件导入 PostgreSQL。txt 文件有 6 列:

Laboratory_Name Laboratory_ID   Facility    ZIP_Code     City   State

And 213 rows.

和 213 行。

I'm trying to use \copyto put the contents of this file into a table called doe2in PostgreSQL using this command:

我正在尝试使用以下命令\copy将此文件的内容放入doe2在 PostgreSQL中调用的表中:

\copy DOE2 FROM '/users/nathangroom/desktop/DOE_inventory5.txt' (DELIMITER(' '))

It gives me this error:

它给了我这个错误:

missing data for column "facility"
missing data for column "facility"

I've looked all around for what to do when encountering this error and nothing has helped. Has anyone else encountered this?

我四处寻找遇到此错误时该怎么办,但没有任何帮助。有人遇到过这种情况么?

回答by Erwin Brandstetter

Three possible causes:

三种可能的原因:

  1. One or more lines of your file has only 4 or fewer space characters (your delimiter).

  2. One or more space characters have been escaped (inadvertently). Maybe with a backslash at the end of an unquoted value. For the (default) textformat you are using, the manual explains:

    Backslash characters (\) can be used in the COPYdata to quote data characters that might otherwise be taken as row or column delimiters.

  1. 文件的一行或多行只有 4 个或更少的空格字符(您的分隔符)。

  2. 一个或多个空格字符已被转义(无意中)。也许在不带引号的值的末尾有一个反斜杠。对于text您使用的(默认)格式,手册解释了:

    \可以在COPY数据中使用反斜杠字符 ( )来引用可能被视为行或列分隔符的数据字符。

Output from COPY TOor pg_dumpwould not exhibit any of these faults when reading from a table with matching layout. But maybe your file has been edited or is from a different, faulty source?

从具有匹配布局的表中读取时,来自COPY TOpg_dump不会出现任何这些错误的输出。但也许您的文件已被编辑或来自不同的、错误的来源?

  1. You are not using the file you thinkyou are using. The \copymeta-command of the psql command-line interface is a wrapper for COPYand reads files local to the client. If your file lives on the server, use the SQL command COPYinstead.
  1. 您没有使用您认为正在使用的文件。\copypsql 命令行界面的元命令是COPY客户端本地文件的封装和读取。如果您的文件位于服务器上,请改用 SQL 命令COPY