在 Psql 输出中禁用换行

Disable wrapping in Psql output

在 Linux 中使用 Psql 时,如果我的 SQL 查询结果包含许多列或长字符串数据,它将环绕初始视图,只有在我滚动到一侧时才会它停止换行并在单独的行上显示每一行。

我尝试了各种 \pset options,例如 format unalignedformat alignedformat wrappedcolumns 0columns 1000,但是 none 似乎 完全 停止换行,除非我生成文件的静态输出。

如何将其设置为从不换行输出,同时仍可滚动并使用默认的 ascii table 格式显示结果?

Psql 使用系统查看器在控制台中显示其输出。在 bash 中,它可能使用 less 作为其提供的 scrollable/page-able 功能。要使用不同的查看器或使用不同的设置,您只需设置 PAGER 环境变量。

运行 psqlless-S--chop-long-lines 选项一起使用似乎对我有用:

PAGER="less -S" psql

您还可以在 less 中查看输出时启用此功能,方法是键入 -SEnter

禁用 select 查询的包装输出。

\pset 寻呼机开启\pset pager off 切换回旧的输出视图。

less 的 -F-S 标志将导致 \d some_table 在某些情况下不显示任何输出。

-F or --quit-if-one-screen
    Causes less to automatically exit if the entire file can be 
    displayed on the first screen.

-S or --chop-long-lines
    Causes lines longer than the screen width to be chopped rather than folded. 
    That is, the portion of a long line that does not fit in the screen width is 
    not shown. The default is to fold long lines; that is, display the remainder 
    on the next line.

像这样使用它们:

PAGER="less -S" psql

似乎更安全,因为不得不手动退出 less 带来的不便。

可能你应该使用 aligned 输出格式:

\pset format aligned

您可以检查所有可用格式以满足您的需要:

\pset format TAB
aligned          html             latex-longtable  unaligned        
asciidoc         latex            troff-ms         wrapped       

您还应该检查 PAGER 在您的环境中配置的值

psql 的默认寻呼机是 less。然后对于没有换行必须使用 less -SPAGER="less -S" psql 是一个很好的方法。但是里面psql可以用

\setenv PAGER 'less -S'