I'm selecting from a table with long text columns. I'd like to wrap long lines to a maximum line length.


test_id |                                  text
      1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lorem


test_id |              text
      1 | Lorem ipsum dolor sit amet,+
        | consectetur adipiscing elit+
        | . Mauris lorem
  • On word boundaries? I don't think that feature is supported.
    – Gaius
    Commented Mar 14, 2011 at 9:40
  • 1
    @Gaius: Ignoring the word boundaries is ok
    – jkj
    Commented Mar 14, 2011 at 10:04
  • Assume you just want to see the result wrapped? Are you executing the query via commandline tool or pgAdmin or phpPgAdmin? Commented Mar 14, 2011 at 11:10
  • @Sandy: psql command line tool
    – jkj
    Commented Mar 14, 2011 at 14:46
  • adjust the size of your terminal window? :D Commented Mar 17, 2011 at 12:36

3 Answers 3


If you're using the psql command line tool, issue this command first:

\pset format wrapped

It should then wrap long lines to your terminal window like so:

test_id |              text
      1 | Lorem ipsum dolor sit amet,.
        |.consectetur adipiscing elit.
        |.. Mauris lorem

You can also set the number of columns to wrap to with

\pset columns 100

and you can change the dots to ellipses with

\pset linestyle unicode

More info: http://www.postgresql.org/docs/current/static/app-psql.html

  • I found using \pset columns 0 after \pset format wrapped worked best; it tells psql to detect the screen width to set the column size, and it seems to do so intelligently, where columns with shorter string values get a smaller width.
    – Randall
    Commented Aug 31, 2022 at 17:39

My answer won't directly answer your question because I don't think psql itself can specifically do this. But, \x will turn on expanded output, which will place your values like this:

-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id         | 1
longstring | This is a long string of text that will be quite long and most likely be very annoying to read if you are viewing results with more than at most a few columns of data. Words words words words words words lorem ipsum.

You can also configure your pager to not wrap lines.

To switch to normal display, just issue the command \x again. Explanation:

\x [on|off|auto] toggle expanded output (currently off)

With the Regexp_Replace function, we can control the exact width of each column separately.  In your example, 27 seems to be the desired column width; so we

SELECT Regexp_Replace('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lorem'
, '(.{27})' , E'\\1\n' , 'g');

, obtaining

 Lorem ipsum dolor sit amet,+
  consectetur adipiscing eli+
 t. Mauris lorem

How it works:    We tell Regexp_Replace to replace a block of 27 consecutive characters (.{27}), with itself (\1) plus a newline (\n); and the global flag (g) says to do that for all such 27-character blocks.
  • 2
    Thanks for the idea. Here is a way to actually make it wrap at spaces: regexp_replace( long_text, '(.{10,20}) ', E'\\1\n ', 'g'). Commented Dec 22, 2020 at 18:41
  • 3
    I like it!! Building on that, here is a way to make text wrap at any word boundaries (so it will split foo:bar into foo: and bar, for example); and without introducing any extra whitespace: Regexp_Replace(long_text, '\y(.{1,27})\y', E'\\1\n', 'g') Commented Dec 24, 2020 at 1:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.