Sub topic 5
Formatting psql output: expanded mode, timing and pager
This chapter assumes you can connect with psql, run SQL, and use basic meta-commands such as \dt and \d. It covers the settings that make query output readable: expanded mode, timing, NULL display, and the pager. It also covers the common ways these settings go wrong.
Why output formatting matters
By default, psql prints results as an aligned table. That works for narrow results. A wide table, such as one with 20 columns, wraps across the terminal and becomes hard to read. A long result scrolls past before you can read it. A NULL prints as an empty cell, which looks the same as an empty string.
psql has display settings for each of these problems. You change them with meta-commands, and the settings last until you disconnect or change them again.
Expanded mode
Expanded mode prints each row as a block of column | value lines instead of one wide row. Toggle it with \x.
\x
SELECT * FROM orders LIMIT 1;The output looks like this:
-[ RECORD 1 ]----+---------------------
order_id | 1001
customer_id | 42
status | shipped
total_amount | 59.90
created_at | 2026-11-02 09:15:00
notes | Each record gets a header line with its number. Column names line up on the left, so you can scan down the values.
\x on its own flips the setting. Run it twice and you are back to normal. You can also be explicit with \x on and \x off, which is safer in scripts because the result does not depend on the current state.
Auto mode
Expanded mode is poor for narrow results, because a two-column table becomes a long list. Auto mode chooses for you.
\x autoWith \x auto, psql uses the normal table when the result fits the terminal width and switches to expanded when it does not. Most people leave auto mode on permanently.
| Command | Behavior |
|---|---|
| `\x on` | Always expanded |
| `\x off` | Always the normal table |
| `\x auto` | Expanded only when the table is wider than the terminal |
| `\x` | Toggles between on and off |
A one-off alternative is to end a query with \gx instead of a semicolon. It runs the query and shows that one result expanded, without changing the session setting.
SELECT * FROM orders WHERE order_id = 1001 \gxTiming
\timing on makes psql print how long each statement took, measured on the client.
\timing on
SELECT count(*) FROM orders; count
--------
250000
(1 row)
Time: 18.402 msRun \timing with no argument to toggle it, or \timing off to turn it off.
Know what this number includes. It is the elapsed time seen by the client: network round trip, server execution, and the time to receive the results. If you connect to a remote server, network latency is part of it. If the result is large, the time to transfer and print rows is part of it. For the server-side execution time alone, use EXPLAIN ANALYZE, covered in a later chapter.
Timing a single run is also unreliable. The first run may be slower because data has to be read from disk, while later runs find it in cache. Run a query a few times before you draw conclusions.
Showing NULL values
A NULL is the absence of a value. By default psql prints it as nothing, so it is indistinguishable from an empty string or a string of spaces. Fix this with \pset null.
\pset null '(null)'
SELECT order_id, notes FROM orders LIMIT 3; order_id | notes
----------+----------
1001 | (null)
1002 | gift
1003 |
(3 rows)Row 1001 has a NULL. Row 1003 has an empty string. Without the setting, both look blank. Choose a marker that cannot be a real value in your data, since psql cannot tell the marker from real text.
The pager
When output is longer than the screen, psql sends it through a pager, a program that shows one screen at a time. By default this is less on most systems. Inside it, press space for the next page, q to quit, and /text to search.
The pager only starts when the output is longer than the terminal height. Control it with \pset pager:
| Setting | Effect |
|---|---|
| `\pset pager on` | Use the pager when output is longer than the screen (the default) |
| `\pset pager off` | Never use a pager; output scrolls past |
| `\pset pager always` | Use the pager for every result |
You can pick a different pager with the PSQL_PAGER or PAGER environment variable. The pspg program is a popular choice for tables, because it freezes the header row and scrolls columns sideways.
export PSQL_PAGER='less -S'
psql -d mydbThe -S option tells less to cut long lines instead of wrapping them. You then scroll sideways with the arrow keys. That combination is a good fit for wide tables when you do not want expanded mode.
Other useful `\pset` options
\pset is the general command for display settings. Type \pset alone to list the current values.
\pset formatchanges the output format. Values includealigned(default),unaligned,csv,html,latex, andwrapped.\pset format csvis handy for pasting results into a spreadsheet.\pset border 0|1|2sets how much of a frame the table has. The default is 1.\pset linestyle unicodedraws the table with line-drawing characters instead of ASCII.\pset nullsets the NULL marker, as above.\pset footer offremoves the(3 rows)line.\pset tuples_only onremoves headers and footers, leaving only data.\tis its shortcut.
Some of these have shortcuts. \a toggles aligned and unaligned output. \H toggles HTML output.
Sending output to a file
Use \o to redirect query results to a file, and \o alone to go back to the screen.
\pset format csv
\o /tmp/orders.csv
SELECT order_id, customer_id, total_amount FROM orders;
\o
\pset format alignedThe \o command writes only query results, not messages. Remember to reset the format afterwards. For real exports, the \copy command is usually a better tool, since it handles quoting and headers directly.
How the settings fit together
Each setting is independent. Together they form a typical exploration session:
Making the settings permanent
Typing these settings every session is tedious. Put them in a ~/.psqlrc file, which psql reads at the start of each interactive session. That file is covered in a later chapter. For now, remember two points. First, psql -X skips it, which is what you want in scripts so that output is the same on every machine. Second, settings made inside a session do not persist after you quit.
Common errors and gotchas
- Expanded mode left on. You toggled
\xearlier and forgot. Narrow results now print as long lists. Use\x autoand stop thinking about it. - Toggle confusion.
\xand\timingwith no argument flip the current state. In a script, useonoroffexplicitly. - Blank cell that is not NULL. Without
\pset null, you cannot tell NULL from an empty string. - Timing includes the network. A fast query on a distant server still shows a slow time.
- Stuck in the pager. Press
q. - Machine-readable output polluted by headers. If a script parses output, combine
-At(unaligned, tuples only) so no headers, footers, or padding appear:psql -X -At -d mydb -c "SELECT count(*) FROM orders". - Settings lost on reconnect.
\cto another database keeps your\psetsettings, but starting a newpsqlprocess does not.
Exercises
Exercise
You run SELECT * FROM orders LIMIT 5; on a table with 25 columns and the output wraps unreadably. Which single setting should you turn on so that wide results switch to a record layout automatically, while narrow results still print as a table?
Show answer
Run \x auto. It prints the normal table when the result fits the terminal width and switches to expanded mode only when it does not.
Exercise
A column notes shows a blank cell for two rows. How can you tell whether each blank is NULL or an empty string?
Show answer
Run \pset null '(null)' and repeat the query. Rows with a NULL now show (null), and rows with an empty string stay blank.
Exercise
With \timing on, a query reports 240 ms against a remote server, but EXPLAIN ANALYZE reports an execution time of 12 ms. Give the likely reason for the difference.
Show answer
\timing measures elapsed time on the client. It includes the network round trip and the time to transfer and print the results, while the EXPLAIN ANALYZE figure covers only server-side execution.