如何在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输出中隐藏结果集修饰
发布评论