postgresql 我们可以使用假脱机命令在 postgres 中导出查询结果吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7414723/
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-20 23:18:08  来源:igfitidea点击:

Can we export query result in postgres using spooling command?

postgresql

提问by DMS

I want to export query result in csv file. I used query select * from entity COPY (select * from entity) TO 'D:/text.csv' WITH DELIMITER AS '|' CSV HEADER; and it workd.. But I want to know if I can do it using spool command like we do it in oracle..

我想将查询结果导出到 csv 文件中。我使用查询 select * from entity COPY (select * from entity) TO 'D:/text.csv' WITH DELIMITER AS '|' CSV 标头;它起作用了..但我想知道我是否可以像我们在 oracle 中那样使用 spool 命令来做到这一点..

回答by Mike Sherrill 'Cat Recall'

PostgreSQL doesn't have a SPOOL command. You can direct output to a file using COPY (as you've discovered) or by using psql. Psql is a command-line tool. It's not a re-implementation of Oracle's SPOOL.

PostgreSQL 没有 SPOOL 命令。您可以使用 COPY(如您所见)或使用 psql 将输出定向到文件。Psql 是一个命令行工具。它不是 Oracle 的 SPOOL 的重新实现。

From the OS prompt

从操作系统提示

psql -o filename -c 'select * from your_table_name;'

You might have to specify the username and database name, depending on how your login is set up.

您可能必须指定用户名和数据库名称,具体取决于您的登录设置。

From the psql prompt

从 psql 提示符

\o filename
select * from your_table_name; 

There are options to control the output. See the -F and -R options, especially.

有控制输出的选项。请参阅 -F 和 -R 选项,尤其是。