如何在Psql输出中隐藏结果集修饰

编程入门 行业动态 更新时间:2024-10-24 14:22:36
本文介绍了如何在Psql输出中隐藏结果集修饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在psql的输出中隐藏列名和行数?

How do you hide the column names and row count in the output from psql?

我正在通过psql使用以下命令运行SQL查询:

I'm running a SQL query via psql with:

psql --user=myuser -d mydb --output=result.txt -c "SELECT * FROM mytable;"

,我期望输出如下:

1,abc 2,def 3,xyz

但我却得到:

id,text ------- 1,abc 2,def 3,xyz (3 rows)

当然,在事实结束后过滤掉前两行和底行并不是不可能的,但是有办法只用psql吗?阅读它的联机帮助页后,我看到了用于控制字段定界符的选项,但没有任何隐藏外来输出的选项。

Of course, it's not impossible to filter the top two rows and bottom row out after the fact, but it there a way to do it with only psql? Reading over its manpage, I see options for controlling the field delimiter, but nothing for hiding extraneous output.

推荐答案

-t 或-仅元组选项:

psql --user=myuser -d mydb --output=result.txt -t -c "SELECT * FROM mytable;"

已编辑(一年后)添加:

您可能还想查看 COPY 命令。我不再需要测试任何PostgreSQL实例,但是我认为您可以按照以下方式编写一些东西:

You also might want to check out the COPY command. I no longer have any PostgreSQL instances handy to test with, but I think you can write something along these lines:

psql --user=myuser -d mydb -c "COPY mytable TO 'result.txt' DELIMITER ','"

(除了 result.txt 必须是绝对路径)。 COPY 命令还支持更智能的CSV格式。请参见其文档。

(except that result.txt will need to be an absolute path). The COPY command also supports a more-intelligent CSV format; see its documentation.

更多推荐

如何在Psql输出中隐藏结果集修饰

本文发布于:2023-10-18 13:19:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504333.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Psql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!