71

When using Psql in Linux, if the result of my SQL query contains many columns or long strings of data, it will wrap the initial view and only once I scroll to the side will it stop wrapping and show each row on a separate line.

I've tried various \pset options such as format unaligned, format aligned, format wrapped, columns 0, columns 1000, but none seemed to fully stop wrapping unless I generate static output to a file.

How can I set it to never wrap the output while still being scrollable and showing the result using the default ascii table format?

1
  • 2
    Checkout pspg, a pager specifically designed for PostgreSQL use. Commented Aug 31, 2020 at 6:39

5 Answers 5

101

Psql uses a system viewer to show its output in the console. In bash it likely uses less for the scrollable/page-able features it provides. To use a different viewer or use different settings, you just need to set the PAGER environment variable.

Running psql to use less with the -S or --chop-long-lines option seemed to work for me:

PAGER="less -S" psql

You can also toggle this feature while viewing output in less by typing -S and Enter.

Sign up to request clarification or add additional context in comments.

2 Comments

psql() ( PAGER="less -S" command psql "$@" )
My PSQL version uses less -S by default it seems, is there anyway to turn on wrapping without turning off pager?
29

To disable the wrapped output of the select query.

\pset pager on and \pset pager off to switch back to the older output view.

1 Comment

This turns off the pager completely, preventing scrolling up and down through large datasets (it will try to print the whole output to the terminal). Using @Silveri's tip still lets you use the pager for scrolling but just prevents wrapping.
26

The default pager for psql is less. Then for no wrapped lines must be use less -S. PAGER="less -S" psql is a good aproach. But inside psql you can use

\setenv PAGER 'less -S'

Then, to toggle it off and back on, use

\pset pager off
\pset pager on

Comments

15

less's -F or -S flag will causes \d some_table to not show any output in some cases.

-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.

Use them like this:

PAGER="less -S" psql

Seems safer at the inconvenience of having to manually exit less.

1 Comment

Thanks! Simply typing -S then enter will disable line wrap while looking at psql query output
10

probably you should use aligned format for output:

\pset format aligned

You can check all available formats to fit your needs:

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

Also you should check PAGER configured value in your environment

3 Comments

aligned inserts the results into an ASCII-art table for me, which for long values means that I get many lines of ----- to create the table header before the actual values. unaligned avoids this.
for me it was better with \pset format wrapped.
you could add -t and it will skip the table headers. And -q to skip some of the other headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.