我在Ubuntu上使用PostgreSQL 8.4。我有一张表,列c1
通过cN
。列足够宽,选择所有列会导致一行查询结果多次换行。因此,输出难以阅读。
当查询结果只构成几行时,如果我可以查看查询结果,使得每行的每列都在一个单独的行上,例如,
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设置可以让我做那样的事情?
我只需要花更多时间盯着文档。这个命令:
\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 |
Postgresql 9.2的新功能; 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
命令添加到主文件夹中的.psqlrc
并重新启动psql,在启动时配置Look under 'Files' section in the psql doc for more info命令。 \x auto
。
〜/ .psqlrc
# \pset format
# \H
# \x
# \pset pager off
你有这么多的选择,你怎么会感到困惑:-)?主要控制措施是:
# \x off;\pset format wrapped
# \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
较新的“\ x auto”选项仅在“需要时”切换到逐行显示。
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
旧的“\ pset format wrapped”类似于它试图在屏幕上整齐地拟合数据,但如果标题不适合则回退到未对齐。这是一个包装的例子:
PAGER
一个有趣的事情是我们可以水平查看表格,而无需折叠。我们可以使用export PAGER='/usr/bin/less -S'
环境变量。 psql使用它。你可以设置
less -S
或者只是Psql Horizontal Display,如果它已经在命令行中可用,如果没有正确的位置。 -S查看展开的线条。您可以传入任何自定义查看器或其他选项。
我在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)。如果您正在使用sudo apt install pspg
,只需安装一个软件包(在类似Debian的发行版上):
qazxswpoi
另外一定要检查\ H,它打开/关闭HTML输出。在控制台上读取并不一定容易,但有趣的是转储到文件中(参见\ o)或粘贴到编辑器/浏览器窗口中进行查看,特别是对于多行相对复杂的数据。
您可以使用zenity将查询输出显示为html表。