【问题标题】:psql /copy “No such file or directory”psql /copy “没有这样的文件或目录”
【发布时间】:2018-05-12 17:00:48
【问题描述】:

我在我的 postgres 服务器上创建了一个名为“new_test”的表,我正在尝试从本地 Mac 上名为“test.csv”的 CSV 文件复制到我的新表。我的“new_test”表和“test.csv”文件都有一个名为“compkey”的列。我在 csv 文件中只有 4 行。可能psql命令如下:

\copy new_test(COMPKEY) from ‘TEST.csv’ with delimiter ‘,’ csv header;

但我不断收到此错误消息:‘TEST.csv’: No such file or directory

我已尝试通过\cd 找到正确的文件路径,并确认我在带有\! ls 的正确文件夹中,看到列出的“TEST.csv”文件。我也试过我的 Mac 上的绝对路径,像这样:

\copy new_test(COMPKEY) from '/Users/the/rest/of/the/file/path/TEST.csv’ with delimiter ‘,’ csv header;

...但我收到相同的错误消息。我做错了什么或错过了什么?

【问题讨论】:

  • @VaoTsun 我看到的唯一区别是使用 COPY 而不是 /copy,对吗?我得到的错误是:ERROR: must be superuser to COPY to or from a file HINT: Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone. 我可以在启动 psql 时尝试sudo 看看它是否有效。
  • 否 - 错误的复制粘贴。请尝试\copy new_test(COMPKEY) from '/Users/the/rest/of/the/file/path/TEST.csv' with delimiter ',' csv header; 并且差异是绝对路径的前导斜杠
  • @VaoTsun 这也适用于我。我没有意识到前面的斜杠是用来表示绝对路径的。

标签: postgresql csv copy psql postgresql-9.6


【解决方案1】:

您在绝对路径中缺少前导斜杠。它应该从根开始。因此以下应该可以工作:

\copy new_test(COMPKEY) from '/Users/the/rest/of/the/file/path/TEST.csv' with delimiter ',' csv header;

文章示例:https://www.linux.com/blog/absolute-path-vs-relative-path-linuxunix

绝对路径定义为指定文件的位置 或根目录(/)中的目录

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2021-06-24
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多