如何将 PostgreSQL 中的结果数据导出为 .CSV?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30130039/
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
How to export the resulting data in PostgreSQL to .CSV?
提问by junior
I use PostgreSQL 9.4.1
我使用 PostgreSQL 9.4.1
My query:
我的查询:
copy(select * from city) to 'C:\temp\city.csv'
copy(select * from city) to E'C:\temp\city.csv'
ERROR: relative path not allowed for COPY to file ********** Error **********
ERROR: relative path not allowed for COPY to file SQL state: 42602
错误:相对路径不允许复制到文件 ************ 错误 **********
错误:不允许将相对路径复制到文件 SQL 状态:42602
采纳答案by Politank-Z
As with this case, it seems likely that you are attempting to use copyfrom a computer other than the one which hosts your database. copydoes I/O from the database host machine's local file system only. If you have access to that filesystem, you can adjust your attempt accordingly. Otherwise, you can use the \copycommand in psql.
与这种情况一样,您似乎很可能正在尝试copy从托管数据库的计算机以外的计算机上使用。copy仅从数据库主机的本地文件系统执行 I/O。如果您有权访问该文件系统,则可以相应地调整您的尝试。否则,你可以使用\复制命令psql。
回答by kidnan1991
It could be late but i think it can be helpful.
可能会迟到,但我认为它会有所帮助。
- On Windows, make sure the output directory has gain the read/write right for Everyone(or you can specific user name).
- Using slash(/) instead of backslash(), example
- 在 Windows 上,确保输出目录已获得每个人的读/写权限(或者您可以指定用户名)。
- 使用slash(/) 而不是 backslash(),示例
COPY DT1111 TO 'D:/TEST/DT1111_POST.CSV' DELIMITER ',' CSV HEADER;
将 DT1111 复制到 'D:/TEST/DT1111_POST.CSV' DELIMITER ',' CSV HEADER;


