Sub topic 5
Formatting psql Output: Expanded Mode, Timing, and Pager
Assume you know basic SELECT statements and can connect to PostgreSQL with psql. This chapter focuses on making query results easier to read and easier to investigate. These settings change how psql displays results; they do not change the stored data.
Why output formatting matters
A normal result uses one row per line and one column per table column. This is useful when the result is narrow:
id | status | total
----+---------+-------
42 | shipped | 89.50Wide tables are harder to read because columns wrap or extend beyond the terminal. Expanded display shows each row vertically, with one column on each line.
-[ RECORD 1 ]----------------
id | 42
status | shipped
total | 89.50Use \x auto to let psql choose automatically. It uses normal display for narrow results and expanded display when the result is too wide.
\x auto
SELECT * FROM orders LIMIT 3;You can also force either mode:
\x on
\x offThe setting remains active for the rest of the session unless you change it.
Measure query time
Timing helps you distinguish a quick query from one that needs investigation. Turn it on with \timing:
\timing on
SELECT * FROM orders WHERE customer_id = 42;After the result, psql prints a duration such as:
Time: 3.421 msTurn it off with \timing off. The displayed time includes client and server work involved in sending and displaying the result, so it is useful for comparison but is not a detailed performance diagnosis. Use EXPLAIN (ANALYZE, BUFFERS) when you need a query plan and execution details.
You can make missing values visible by changing the NULL marker:
\pset null '(null)'This prevents an empty cell from being confused with an empty string.
Control the pager
A pager is a terminal program, commonly less, that displays long output one screen at a time. It prevents a large result from scrolling past before you can read it.
\pset pager onWith the default \pset pager auto, psql uses the pager only when output is longer than the terminal window. You can also disable it:
\pset pager offInside the pager, use arrow keys or Page Up and Page Down to move. Press q to return to the psql prompt. Avoid sending large exploratory queries to the screen without a limit. The pager helps you inspect output, but it does not reduce the amount of data PostgreSQL sends.
| Setting | Command | Typical use |
|---|---|---|
| Expanded display | `\x auto` | Wide rows |
| Query timing | `\timing on` | Comparing durations |
| Pager | `\pset pager auto` | Long output |
| NULL marker | `\pset null '(null)'` | Distinguishing NULL values |
Exercise
Which commands show a wide result vertically, display query duration, and disable the pager?
Show answer
Use \x on, \timing on, and \pset pager off.