ChatGPT解决这个技术问题 Extra ChatGPT

psql 的替代输出格式

我在 Ubuntu 上使用 PostgreSQL 8.4。我有一个包含 c1cN 列的表。这些列足够宽,以至于选择所有列会导致一行查询结果多次换行。因此,输出很难阅读。

当查询结果仅包含几行时,如果我可以查看查询结果以使每行的每一列都在单独的行上,那将很方便,例如

 c1: <value of row 1's c1>
 c2: <value of row 1's c1>
 ...
 cN: <value of row 1's cN>
 ---- some kind of delimiter ----
 c1: <value of row 2's c1>
 etc.

我在不希望安装任何其他软件的服务器上运行这些查询。是否有一个 psql 设置可以让我做这样的事情?


u
user100464

我只需要花更多时间盯着文档。这个命令:

\x on

会做我想要的。这是一些示例输出:

select * from dda where u_id=24 and dda_is_deleted='f';
-[ RECORD 1 ]------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
dda_id             | 1121
u_id               | 24
ab_id              | 10304
dda_type           | CHECKING
dda_status         | PENDING_VERIFICATION
dda_is_deleted     | f
dda_verify_op_id   | 44938
version            | 2
created            | 2012-03-06 21:37:50.585845
modified           | 2012-03-06 21:37:50.593425
c_id               | 
dda_nickname       | 
dda_account_name   | 
cu_id              | 1
abd_id             | 

您也可以尝试 \pset 格式包装(允许的格式是未对齐、对齐、包装、html、latex、troff-ms)。如果显示足够窄,它将自动换行每一列。
\x 将切换值,为您节省 3 次击键! (当您在宽输出和窄输出之间快速切换时很有用)。
如果您从 Mysql 中寻找与 \G 等效的内容,请尝试将 \x\g\x 附加到查询末尾或在 ~/.psqlrc 中添加 \set G '\\set QUIET 1\\x\\g\\x\\set QUIET 0' 定义快捷方式,然后在末尾使用 :G。 (注意缺少分号)
是否可以在命令行以某种方式传递“\x on”或等效项?如果我可以保存(例如,通过使用 bash shell 别名)psql --something 以便我始终默认打开该选项,那就太好了。
@machineghost 使用 -P expanded=auto--pset expanded=auto
n
notapatch

(新)扩展自动模式:\x auto

Postgresql 9.2 的新功能; PSQL 自动使记录适应屏幕的宽度。以前您只有打开或关闭扩展模式,并且必须根据需要在模式之间切换。

如果记录可以适应屏幕的宽度; psql 使用普通格式。

如果记录不能适应屏幕的宽度; psql 使用扩展模式。

要获得此用途: \x auto

Postgresql 9.5 Documentation on PSQL command.

宽屏,正常格式:

 id | time  |       humanize_time             | value 
----+-------+---------------------------------+-------
  1 | 09:30 |  Early Morning - (9.30 am)      |   570
  2 | 11:30 |  Late Morning - (11.30 am)      |   690
  3 | 13:30 |  Early Afternoon - (1.30pm)     |   810
  4 | 15:30 |  Late Afternoon - (3.30 pm)     |   930
(4 rows)

窄屏,扩展格式:

-[ RECORD 1 ]-+---------------------------
id            | 1
time          | 09:30
humanize_time | Early Morning - (9.30 am)
value         | 570
-[ RECORD 2 ]-+---------------------------
id            | 2
time          | 11:30
humanize_time | Late Morning - (11.30 am)
value         | 690
-[ RECORD 3 ]-+---------------------------
id            | 3
time          | 13:30
humanize_time | Early Afternoon - (1.30pm)
value         | 810
-[ RECORD 4 ]-+---------------------------
id            | 4
time          | 15:30
humanize_time | Late Afternoon - (3.30 pm)
value         | 930

如何使用 \x auto 启动 psql?

在启动时配置 \x auto 命令,方法是将其添加到主文件夹中的 .psqlrc 并重新启动 psql。 Look under 'Files' section in the psql doc for more info

~/.psqlrc

\x auto

什么选项可用于将 normal formatted 表完全包含在 dashes 中?我也喜欢外包装盒,MySQL 就是这样做的。
B
Bryce

你有这么多选择,你怎么会感到困惑:-)?主要控制有:

# \pset format
# \H
# \x
# \pset pager off

每个人都有选择和与其他人的互动。最自动的选项是:

# \x off;\pset format wrapped
# \x auto

较新的“\x auto”选项仅在“需要时”切换到逐行显示。

-[ RECORD 1 ]---------------
id          | 6
description | This is a gallery of oilve oil brands.
authority   | I love olive oil, and wanted to create a place for
reviews and comments on various types.
-[ RECORD 2 ]---------------
id          | 19
description | XXX Test A 
authority   | Testing

旧的“\pset format Wrapped”类似,它试图将数据整齐地放在屏幕上,但如果标题不适合,则会退回到未对齐的状态。这是一个包装的例子:

 id |          description           |            authority            
----+--------------------------------+---------------------------------
  6 | This is a gallery of oilve     | I love olive oil, and wanted to
    ; oil brands.                    ;  create a place for reviews and
    ;                                ;  comments on various types.
 19 | Test Test A                    | Testing

\x auto\pset pager off 是一个不错的选择。
\x off; \pset format wrapped\x auto 是要一起使用还是相互排斥?
s
spazm

一件有趣的事情是我们可以水平查看表格,而无需折叠。我们可以使用 PAGER 环境变量。 psql 使用它。你可以设置

export PAGER='/usr/bin/less -S'

或者如果它已经在命令行中可用,则只是 less -S,如果没有正确的位置。 -S 查看展开的线条。您可以使用它传入任何自定义查看器或其他选项。

我在Psql Horizontal Display中写了更多


答案真的很好。我很惊讶它没有被标记得更高。非常适合在终端工作的任何人。
这非常有帮助
这很有用,但是如何向右滚动?
@NieldeWet 对我来说使用左右箭头水平滚动有效。但我希望能够使用 VIM 绑定来做到这一点,但它确实有效。
T
Tombart

pspg 是一个简单的工具,提供高级表格格式、水平滚动、搜索和更多功能。

git clone https://github.com/okbob/pspg.git
cd pspg
./configure
make
make install

然后确保更新 PAGER 变量,例如在您的 ~/.bashrc

export PAGER="pspg -s 6" 

其中 -s 代表配色方案 (1-14)。如果您使用 pgdg repositories,只需安装一个软件包(在类似 Debian 的发行版上):

sudo apt install pspg

https://i.stack.imgur.com/JGYC8.png


W
Wexxor

还请务必查看 \H,它可以打开/关闭 HTML 输出。在控制台上不一定容易阅读,但对于转储到文件中(参见 \o)或粘贴到编辑器/浏览器窗口进行查看很有趣,尤其是对于多行相对复杂的数据。


N
Nam G VU

如果您正在寻找像我一样的 psql command-line mode

here 是语法 --pset expanded=auto

psql 命令行选项: -P 扩展=auto --pset 扩展=auto -x --expanded ...

另一种方法是 -q 选项 ref


p
purushothaman poovai

您可以使用 zenity 将查询输出显示为 html 表。

首先使用以下代码实现 bash 脚本: cat > '/tmp/sql.op'; zenity --text-info --html --filename='/tmp/sql.op';像 mypager.sh 一样保存它

然后通过将脚本的完整路径设置为值来导出环境变量 PAGER。例如:- export PAGER='/path/mypager.sh'

然后登录到psql程序然后执行命令\H

最后执行任何查询,表格输出将以html表格格式显示在zenity中。