use*_*464 286 postgresql output-formatting psql
我在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.
Run Code Online (Sandbox Code Playgroud)
我在服务器上运行这些查询,我不想安装任何其他软件.是否有psql设置可以让我做那样的事情?
use*_*464 489
我只需要花更多时间盯着文档.这个命令:
\x on
Run Code Online (Sandbox Code Playgroud)
会做我想要的.这是一些示例输出:
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 |
Run Code Online (Sandbox Code Playgroud)
not*_*tch 253
Postgresql 9.2的新功能; PSQL自动将记录拟合到屏幕宽度.以前您只能打开或关闭扩展模式,必须根据需要在模式之间切换.
要使用它:\ x auto
宽屏,普通格式:
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)
Run Code Online (Sandbox Code Playgroud)
窄屏,扩展格式:
-[ 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
Run Code Online (Sandbox Code Playgroud)
\x auto通过将命令添加到.psqlrc主文件夹并重新启动psql,在启动时配置命令.有关详细信息,请查看psql文档中的"文件"部分.
〜/ .psqlrc
\x auto
Run Code Online (Sandbox Code Playgroud)
Bry*_*yce 62
你有这么多的选择,你怎么会感到困惑:-)?主要控制措施是:
# \pset format
# \H
# \x
# \pset pager off
Run Code Online (Sandbox Code Playgroud)
每个人都有选择和与他人的互动.最自动的选择是:
# \x off;\pset format wrapped
# \x auto
Run Code Online (Sandbox Code Playgroud)
较新的"\ 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
Run Code Online (Sandbox Code Playgroud)
较旧的"\ 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
Run Code Online (Sandbox Code Playgroud)
Din*_*mar 15
一个有趣的事情是我们可以水平查看表格,而无需折叠.我们可以使用PAGER环境变量.psql使用它.你可以设置
export PAGER='/usr/bin/less -S'
Run Code Online (Sandbox Code Playgroud)
或者只是less -S如果它已经在命令行中可用,如果没有正确的位置.-S查看展开的线条.您可以传入任何自定义查看器或其他选项.
我在Psql Horizontal Display中写了更多
Tom*_*art 14
pspg 是一个简单的工具,提供高级表格格式,水平滚动,搜索和更多功能.
git clone https://github.com/okbob/pspg.git
cd pspg
./configure
make
make install
Run Code Online (Sandbox Code Playgroud)
然后确保更新PAGER变量,例如在你的~/.bashrc
export PAGER="pspg -s 6"
Run Code Online (Sandbox Code Playgroud)
其中-s代表配色方案(1-14).如果你正在使用pgdg存储库,只需安装一个包(在类似Debian的发行版上):
sudo apt install pspg
Run Code Online (Sandbox Code Playgroud)
另外一定要检查\ H,它打开/关闭HTML输出.在控制台上读取并不一定容易,但有趣的是转储到文件中(参见\ o)或粘贴到编辑器/浏览器窗口中进行查看,特别是对于多行相对复杂的数据.
| 归档时间: |
|
| 查看次数: |
171361 次 |
| 最近记录: |