26

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

From:

SELECT * FROM test;
test_id |                                  text
--------+-----------------------------------------------------------------------
      1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lorem

To:

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 Mar 14 '11 at 9:40
  • @Gaius: Ignoring the word boundaries is ok – jkj Mar 14 '11 at 10:04
  • Assume you just want to see the result wrapped? Are you executing the query via commandline tool or pgAdmin or phpPgAdmin? – CoderHawk Mar 14 '11 at 11:10
  • @Sandy: psql command line tool – jkj Mar 14 '11 at 14:46
  • adjust the size of your terminal window? :D – Derek Downey Mar 17 '11 at 12:36
25

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/9.3/static/app-psql.html

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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