psql的备用输出格式

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)

  • 单独的`\ x`将切换值,为您节省三次击键!(当您在宽输出和窄输出之间快速切换时很有用). (11认同)
  • 你也可以尝试\ pset格式包装(允许的格式是未对齐,对齐,包装,html,乳胶,troff-ms).如果显示器足够窄,它将自动换行每列. (7认同)
  • 如果你从Mysql中寻找相当于`\ G`的命令,请尝试将`\ x\g\x`添加到查询结尾或在`〜/ .psqlrc`中定义快捷键添加`\ set G'\\ set QUIET 1\\ x \\ g \\ x \\设置QUIET 0',然后在末尾使用`:G`.(注意缺少分号) (7认同)
  • 有可能以某种方式传递"`\ x on`"或命令行中的等价物吗?如果我可以保存(例如,通过使用bash shell别名)`psql --something`,那么我总是默认启用该选项. (5认同)
  • @machineghost使用`-P expand = auto`或`--pset Expanded = auto` (2认同)

not*_*tch 253

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

Postgresql 9.2的新功能; PSQL自动将记录拟合到屏幕宽度.以前您只能打开或关闭扩展模式,必须根据需要在模式之间切换.

  • 如果记录可以适合屏幕的宽度; psql使用普通格式.
  • 如果记录不能适合屏幕的宽度; psql使用扩展模式.

要使用它:\ x auto

关于PSQL命令的Postgresql 9.5文档.


宽屏,普通格式:

 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启动psql?

\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)

  • 披露:我撰写了\ pset格式包装 (14认同)

Din*_*mar 15

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

export PAGER='/usr/bin/less -S'
Run Code Online (Sandbox Code Playgroud)

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

我在Psql Horizo​​ntal Display中写了更多

  • 答案真的很好。我很惊讶它没有标记更高。适合在终端工作的任何人。 (2认同)

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)

pspg的例子


Nam*_* VU 11

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

是语法--pset expanded=auto

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

另一种方法是-q选项ref


Wex*_*xor 7

另外一定要检查\ H,它打开/关闭HTML输出.在控制台上读取并不一定容易,但有趣的是转储到文件中(参见\ o)或粘贴到编辑器/浏览器窗口中进行查看,特别是对于多行相对复杂的数据.