如何在 psql 中设置最大列宽?

How do I set maximum column width in psql?

如何运行在psql中分类多个长列?我有多个列,其中一些很长,我想将它们全部轻松安装在一个终端中 window。 \pset format wrapped\pset columns COLUMNS 似乎适用于某些情况,但并非适用于所有情况。我主要关心 character varying 列,它似乎没有限制它们的长度或正确包装它们。它仅适用于 1 个字符串的简单情况。

我知道 substr()\x,但这些都不是最佳选择。我有很多专栏,所以每个专栏的 substr() 太重复了。表格输出在这里最适合我,所以 \x 不那么容易查看。

示例:

正确设置寻呼机 运行 psql:

export PAGER=less
export LESS='-iMSx4 -RSFX -e'
psql -d db_foo

设置仅适用于最简单的情况:

db_foo=# \pset format wrapped
Output format is wrapped.
db_foo=# \pset columns 5
Target width is 5.
db_foo=# select '01234456789 01234456789 01234456789' as foo;
 foo 
-----
 012.
.344.
.567.
.89 .
.012.
...
(1 row)

 -- OK, output is truncated.

db_foo=# select baz from table_baz limit 2;       
             baz              
-----------------------------------
 Salmon Lightweight, Galaxy v1.0.0
 Salmon quant Galaxy v0.14.1.2
(2 rows)

-- not OK, output is not truncated.

db_foo=# select '01234456789 01234456789 01234456789' as foo, baz from table_baz limit 2;
                 foo                 |             baz              
-------------------------------------+-----------------------------------
 01234456789 01234456789 01234456789 | Salmon Lightweight, Galaxy v1.0.0
 01234456789 01234456789 01234456789 | Salmon quant Galaxy v0.14.1.2
(2 rows)

-- not OK, output is not truncated - even the strings 
-- that were truncated before.

我运行正在使用 macOS 10.14.6,使用:

psql --version
psql (PostgreSQL) 12.3

less --version
less 487 (POSIX regular expressions)

相关:

How to limit the maximum display length of a column in PostgreSQL
How do I change the max column width in PostgreSQL?
Is there a way to set the max width of a column when displaying JSONB results in psql?

另请参见:

PostgreSQL: Documentation: psql

columns
Sets the target width for the wrapped format, and also the width limit for determining whether output is wide enough to require the pager or switch to the vertical display in expanded auto mode. Zero (the default) causes the target width to be controlled by the environment variable COLUMNS, or the detected screen width if COLUMNS is not set. In addition, if columns is zero then the wrapped format only affects screen output. If columns is nonzero then file and pipe output is wrapped to that width as well.

问题来自 \pset wrapped(在此处描述 psql):

...Note that psql will not attempt to wrap column header titles; therefore, wrapped format behaves the same as aligned if the total width needed for column headers exceeds the target.

columns 值设置为超过 header 标题宽度的值。