如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:54:02  来源:igfitidea点击:

How to export the resulting data in PostgreSQL to .CSV?

postgresqlexport-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.

可能会迟到,但我认为它会有所帮助。

  1. On Windows, make sure the output directory has gain the read/write right for Everyone(or you can specific user name).
  2. Using slash(/) instead of backslash(), example
  1. 在 Windows 上,确保输出目录已获得每个人读/写权限(或者您可以指定用户名)。
  2. 使用slash(/) 而不是 backslash(),示例

COPY DT1111 TO 'D:/TEST/DT1111_POST.CSV' DELIMITER ',' CSV HEADER;

将 DT1111 复制到 'D:/TEST/DT1111_POST.CSV' DELIMITER ',' CSV HEADER;

回答by Do Nhu Vy

I am using pgAdmin v1.5 . The first query is

我正在使用 pgAdmin v1.5 。第一个查询是

select table_name from information_schema.tables where table_catalog = 'ofbiz' order by table_name

Then I press button download, pgAdmin will return a csv file, is result set of first query. enter image description here

然后我按下按钮下载,pgAdmin 将返回一个 csv 文件,是第一次查询的结果集。 在此处输入图片说明