简体   繁体   English

为什么 psql 将 COPY 之后的语句视为标准输入?

[英]Why does psql treat statements after COPY as stdin?

Example script:示例脚本:

CREATE TEMPORARY TABLE foo(col1 text);
COPY foo from STDIN DELIMITER E'\t' CSV HEADER;

SELECT * FROM foo;

Execution:执行:

psql --host=localhost --dbname=postgres --username=postgres \
  --file=my-script.sql < my-data.tsv

When I run this, something peculiar happens.当我运行它时,会发生一些奇怪的事情。 SELECT * FROM foo doesn't run, and instead everything below the COPY statement is treated as rows to be entered into my table. SELECT * FROM foo没有运行,而是COPY语句下面的所有内容都被视为要输入到我的表中的行。

How can I write a SQL script that imports a data.table from standard input and does further processing with it?我如何编写 SQL 脚本,从标准输入导入 data.table 并对其进行进一步处理?

psql treats --file scripts as if they were standard input, so it's expecting your data set to immediately follow the COPY statement. psql 将--file脚本视为标准输入,因此它期望您的数据集紧跟在COPY语句之后。 There is a workaround, as per this email on the Postgres mailing list:根据 Postgres 邮件列表中的email ,有一个解决方法:

cat my-data.tsv | psql -c "$(cat my-script.sql)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM