Alternate output format for psql Alternate output format for psql postgresql postgresql

Alternate output format for psql


I just needed to spend more time staring at the documentation. This command:

\x on

will do exactly what I wanted. Here is some sample output:

select * from dda where u_id=24 and dda_is_deleted='f';-[ RECORD 1 ]------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------dda_id             | 1121u_id               | 24ab_id              | 10304dda_type           | CHECKINGdda_status         | PENDING_VERIFICATIONdda_is_deleted     | fdda_verify_op_id   | 44938version            | 2created            | 2012-03-06 21:37:50.585845modified           | 2012-03-06 21:37:50.593425c_id               | dda_nickname       | dda_account_name   | cu_id              | 1abd_id             | 


(New) Expanded Auto Mode: \x auto

New for Postgresql 9.2; PSQL automatically fits records to the width of the screen. previously you only had expanded mode on or off and had to switch between the modes as necessary.

  • If the record can fit into the width of the screen; psql uses normal formatting.
  • If the record can not fit into the width of the screen; psql uses expanded mode.

To get this use: \x auto

Postgresql 9.5 Documentation on PSQL command.


Wide screen, normal formatting:

 id | time  |       humanize_time             | value ----+-------+---------------------------------+-------  1 | 09:30 |  Early Morning - (9.30 am)      |   570  2 | 11:30 |  Late Morning - (11.30 am)      |   690  3 | 13:30 |  Early Afternoon - (1.30pm)     |   810  4 | 15:30 |  Late Afternoon - (3.30 pm)     |   930(4 rows)

Narrow screen, expanded formatting:

-[ RECORD 1 ]-+---------------------------id            | 1time          | 09:30humanize_time | Early Morning - (9.30 am)value         | 570-[ RECORD 2 ]-+---------------------------id            | 2time          | 11:30humanize_time | Late Morning - (11.30 am)value         | 690-[ RECORD 3 ]-+---------------------------id            | 3time          | 13:30humanize_time | Early Afternoon - (1.30pm)value         | 810-[ RECORD 4 ]-+---------------------------id            | 4time          | 15:30humanize_time | Late Afternoon - (3.30 pm)value         | 930

How to start psql with \x auto?

Configure \x auto command on startup by adding it to .psqlrc in your home folder and restarting psql. Look under 'Files' section in the psql doc for more info.

~/.psqlrc

\x auto


You have so many choices, how could you be confused :-)? The main controls are:

# \pset format# \H# \x# \pset pager off

Each has options and interactions with the others. The most automatic options are:

# \x off;\pset format wrapped# \x auto

The newer "\x auto" option switches to line-by-line display only "if needed".

-[ RECORD 1 ]---------------id          | 6description | This is a gallery of oilve oil brands.authority   | I love olive oil, and wanted to create a place forreviews and comments on various types.-[ RECORD 2 ]---------------id          | 19description | XXX Test A authority   | Testing

The older "\pset format wrapped" is similar in that it tries to fit the data neatly on screen, but falls back to unaligned if the headers won't fit. Here's an example of wrapped:

 id |          description           |            authority            ----+--------------------------------+---------------------------------  6 | This is a gallery of oilve     | I love olive oil, and wanted to    ; oil brands.                    ;  create a place for reviews and    ;                                ;  comments on various types. 19 | Test Test A                    | Testing