我有一些从 postgres 导出的数据,使用电子表格进行了一些修改,我知道希望将数据放回到表中,但我总是在导入时失败:
cat extract.csv | psql -h 10.135.0.44 myapp myapp -f copy-user.sql`
psql:copy-user.sql:7: ERROR: missing data for column "email"
CONTEXT: COPY to_update, line 1: ""
下面提供了实际数据。我首先将 CSV 文件从 DOS 转换为 Unix 风格的行结尾。好像也没有多大关系。
复制用户.sql
COPY "to_update"
FROM STDIN
WITH DELIMITER ';' CSV;
提取.csv
bfb92e29-1d2c-45c4-b9ab-357a3ac7ad13;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f1e0f6f1c5f1e0f6f1bcb5b7b6bcb5b7b6b2bdb6b1b0b2bdb1b6abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a>;x
aeccc3ea-cc1f-43ef-99ff-e389d5d63b22;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f0e1f7f0e1f6c4f0e1f7f0e1f6efeeeae5e1e2e3eeeaf3e1f6e3aaeaeb" rel="noreferrer noopener nofollow">[email protected]</a>;x
9cec13ae-c880-4371-9b1c-dd201f5cf233;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40222c2f222c2f00272d21292c6e232f2d" rel="noreferrer noopener nofollow">[email protected]</a>;x
aeada2bc-a362-4f3e-80f2-06a717206802;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04726170446369656d682a676b69" rel="noreferrer noopener nofollow">[email protected]</a>;x
fb85ddd8-7d17-4d41-8bc3-213b1e469506;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4cac5d2cacac5d2cac1d7c1cae4d4d0c2c8cbd38ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>;x
528e1f2e-1baa-483b-bc8c-85f993014696;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="117a7a7d7a51797e657c70787d3f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>;x
dbc8a9c1-56cf-4589-8b2c-cf1a2e0832ed;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e5eaebebebebc2eaedf6efe3ebeeace1edef" rel="noreferrer noopener nofollow">[email protected]</a>;x
fbf23553-baa2-410a-8f96-32b5c4deb0c7;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0dcd1dcd1f0dcd1dcd19ededf" rel="noreferrer noopener nofollow">[email protected]</a>;x
e22ec0de-06f9-428a-aa3e-171c38f9a1f7;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f470d7f58525e5653115c5052" rel="noreferrer noopener nofollow">[email protected]</a>;x
8e8d0f73-8eb7-43b4-8019-b79042731b97;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a272b23260a272b232664292527" rel="noreferrer noopener nofollow">[email protected]</a>;x
to_update
的表定义
create table to_update(id text, email text, text char);
-- also tried this variant, but same error
-- create table to_update(id uuid, email text, text char);
编辑:附加信息
看来这完全相同的事情不会出现在我的本地计算机上:
$ cat extract.csv | psql postgres -f copy-user.sql
Timing is on.
Line style is unicode.
Border style is 2.
Null display is "[NULL]".
Expanded display is used automatically.
COPY 0
Time: 0.430 ms
它仍然不起作用(因为它只复制 0 行),但至少它不会抛出错误。这表明它与环境(版本、区域设置等)有关。
本地计算机(不会引发错误)
$ psql --version
psql (PostgreSQL) 10.6
$ psql postgres -c "SHOW server_version;"
Timing is on.
Line style is unicode.
Border style is 2.
Null display is "[NULL]".
Expanded display is used automatically.
┌────────────────┐
│ server_version │
├────────────────┤
│ 10.6 │
└────────────────┘
(1 row)
Time: 40.960 ms
$ printenv | grep LC
LC_CTYPE=UTF-8
远程服务器(抛出错误)
$ psql --version # this is the client, not the same physical server as the db
psql (PostgreSQL) 9.5.12
$ psql -h 10.135.0.44 myapp myapp -c "SHOW server_version;"
Password for user pete:
server_version
----------------
9.5.12
(1 row)
$ printenv | grep LC
LC_ALL=C.UTF-8
LC_CTYPE=UTF-8
LANG=C.UTF-8
extract.csv 的十六进制转储(全部 7 行)
$ wc -l extract.csv
10 extract.csv
$ hexdump -C extract.csv
00000000 62 66 62 39 32 65 32 39 2d 31 64 32 63 2d 34 35 |bfb92e29-1d2c-45|
00000010 63 34 2d 62 39 61 62 2d 33 35 37 61 33 61 63 37 |c4-b9ab-357a3ac7|
00000020 61 64 31 33 3b 74 65 73 74 40 74 65 73 74 39 30 |ad13;test@test90|
00000030 32 33 39 30 32 33 37 38 33 34 35 37 38 34 33 2e |239023783457843.|
00000040 63 6f 6d 3b 78 0a 61 65 63 63 63 33 65 61 2d 63 |com;x.aeccc3ea-c|
00000050 63 31 66 2d 34 33 65 66 2d 39 39 66 66 2d 65 33 |c1f-43ef-99ff-e3|
00000060 38 39 64 35 64 36 33 62 32 32 3b 74 65 73 74 65 |89d5d63b22;teste|
00000070 72 40 74 65 73 74 65 72 6b 6a 6e 61 65 66 67 6a |r@testerkjnaefgj|
00000080 6e 77 65 72 67 2e 6e 6f 3b 78 0a 39 63 65 63 31 |nwerg.no;x.9cec1|
00000090 33 61 65 2d 63 38 38 30 2d 34 33 37 31 2d 39 62 |3ae-c880-4371-9b|
000000a0 31 63 2d 64 64 32 30 31 66 35 63 66 32 33 33 3b |1c-dd201f5cf233;|
000000b0 62 6c 6f 62 6c 6f 40 67 6d 61 69 6c 2e 63 6f 6d |<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e0eeede0eeedc2e5efe3ebeeace1edef" rel="noreferrer noopener nofollow">[email protected]</a>|
000000c0 3b 78 0a 61 65 61 64 61 32 62 63 2d 61 33 36 32 |;x.aeada2bc-a362|
000000d0 2d 34 66 33 65 2d 38 30 66 32 2d 30 36 61 37 31 |-4f3e-80f2-06a71|
000000e0 37 32 30 36 38 30 32 3b 76 65 74 40 67 6d 61 69 |7206802;vet@gmai|
000000f0 6c 2e 63 6f 6d 3b 78 0a 66 62 38 35 64 64 64 38 |l.com;x.fb85ddd8|
00000100 2d 37 64 31 37 2d 34 64 34 31 2d 38 62 63 33 2d |-7d17-4d41-8bc3-|
00000110 32 31 33 62 31 65 34 36 39 35 30 36 3b 6e 61 76 |213b1e469506;nav|
00000120 6e 6e 61 76 6e 65 73 65 6e 40 70 74 66 6c 6f 77 |nnavnesen@ptflow|
00000130 2e 63 6f 6d 3b 78 0a 35 32 38 65 31 66 32 65 2d |.com;x.528e1f2e-|
00000140 31 62 61 61 2d 34 38 33 62 2d 62 63 38 63 2d 38 |1baa-483b-bc8c-8|
00000150 35 66 39 39 33 30 31 34 36 39 36 3b 6b 6b 6c 6b |5f993014696;kklk|
00000160 40 68 6f 74 6d 61 69 6c 2e 63 6f 6d 3b 78 0a 64 |@hotmail.com;x.d|
00000170 62 63 38 61 39 63 31 2d 35 36 63 66 2d 34 35 38 |bc8a9c1-56cf-458|
00000180 39 2d 38 62 32 63 2d 63 66 31 61 32 65 30 38 33 |9-8b2c-cf1a2e083|
00000190 32 65 64 3b 67 68 69 69 69 69 40 68 6f 74 6d 61 |2ed;ghiiii@hotma|
000001a0 69 6c 2e 63 6f 6d 3b 78 0a 66 62 66 32 33 35 35 |il.com;x.fbf2355|
000001b0 33 2d 62 61 61 32 2d 34 31 30 61 2d 38 66 39 36 |3-baa2-410a-8f96|
000001c0 2d 33 32 62 35 63 34 64 65 62 30 63 37 3b 6c 61 |-32b5c4deb0c7;la|
000001d0 6c 61 40 6c 61 6c 61 2e 6e 6f 3b 78 0a 65 32 32 |<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04686544686568652a6a6b" rel="noreferrer noopener nofollow">[email protected]</a>;x.e22|
000001e0 65 63 30 64 65 2d 30 36 66 39 2d 34 32 38 61 2d |ec0de-06f9-428a-|
000001f0 61 61 33 65 2d 31 37 31 63 33 38 66 39 61 31 66 |aa3e-171c38f9a1f|
00000200 37 3b 78 32 40 67 6d 61 69 6c 2e 63 6f 6d 3b 78 |7;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbc389fbdcd6dad2d795d8d4d6" rel="noreferrer noopener nofollow">[email protected]</a>;x|
00000210 0a 38 65 38 64 30 66 37 33 2d 38 65 62 37 2d 34 |.8e8d0f73-8eb7-4|
00000220 33 62 34 2d 38 30 31 39 2d 62 37 39 30 34 32 37 |3b4-8019-b790427|
00000230 33 31 62 39 37 3b 6d 61 69 6c 40 6d 61 69 6c 2e |31b97;mail@mail.|
00000240 63 6f 6d 3b 78 0a |com;x.|
00000246
最佳答案
我想你想要\copy ... from pstdin...
在一条线上。起始反斜杠和 pstdin
而不是stdin
是故意的。
此邮件列表线程:psql -f COPY from STDIN解释了问题和解决方案。
COPY FROM STDIN
期望在 COPY 命令之后内联数据(如转储文件中那样),而不是来自 psql 进程的标准输入。
邮件列表中总结替代方案的相关片段
I'd like the store the COPY command in a separate file without specifying an input file name. I want to feed it the data from the shell script that calls psql
“STDIN:所有行均从发出该命令的同一源读取 命令” - 据我现在了解,这适用于 COPY 和\COPY。换句话说,输入文件必须包含命令和数据。
我找到了一些解决方案来实现我的目标:
1) 使用 COPY FROM STDIN cat event.csv | psql -c "$(cat event.sql)"
2) 使用 COPY FROM STDIN psql -f <(cat event.sql event.csv)
3) 使用\COPY FROM PSTDIN cat event.csv | psql -f 事件.sql
4) 使用\COPY FROM STDIN psql -f <(cat event.sql event.csv <(echo “。”))
我不喜欢\COPY 的是它必须在一行上。的确 它不能分成多行
block 引用>
关于postgresql - 从标准输入复制 CSV 文件会抛出 "missing data for column",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384496/