When working with database queries in command-line interfaces, the default tabular output format can become unwieldy with wide tables or many columns. MySQL's \\G
delimiter provides a vertical display format that's often more readable:
mysql> SELECT * FROM users LIMIT 1\G *************************** 1. row *************************** id: 1 username: johndoe email: john@example.com created_at: 2023-01-15 09:30:22
PostgreSQL offers several ways to achieve similar vertical formatting:
1. Using the \\x
Command
The most direct equivalent is PostgreSQL's \\x
(expanded display) command:
psql=# \\x Expanded display is on. psql=# SELECT * FROM users LIMIT 1; -[ RECORD 1 ]----------- id | 1 username | johndoe email | john@example.com created_at | 2023-01-15 09:30:22
You can toggle expanded display with \\x auto
which automatically switches to vertical format for wide results.
2. Using the \\pset
Command
For more control over output formatting:
psql=# \\pset format aligned psql=# \\pset expanded on
For even more control, consider these approaches:
Custom Formatting with \\H
Generate HTML output:
psql=# \\H psql=# SELECT * FROM users LIMIT 1;
Using \\o
to Redirect Output
Combine with formatting commands to save nicely formatted output:
psql=# \\o output.txt psql=# \\x auto psql=# SELECT * FROM users; psql=# \\o
For application-level formatting, consider these SQL-level approaches:
Using jsonb_pretty()
SELECT jsonb_pretty(to_jsonb(users.*)) FROM users LIMIT 1;
Custom Function for Vertical Output
CREATE OR REPLACE FUNCTION vertical_output(query text) RETURNS SETOF text AS $$ DECLARE rec record; col text; BEGIN FOR rec IN EXECUTE query LOOP RETURN NEXT '***************************'; FOR col IN SELECT column_name FROM information_schema.columns WHERE table_name = split_part(query, ' ', 3) LOOP RETURN NEXT format('%20s | %s', col, rec.col); END LOOP; END LOOP; RETURN; END; $$ LANGUAGE plpgsql;
While PostgreSQL doesn't have an exact \\G
equivalent, its \\x
provides similar functionality with additional benefits:
- Persistent between queries (unlike MySQL's per-query
\\G
) - Configurable auto-switching behavior
- More formatting options through
\\pset
When working with wide tables in MySQL, the \G
terminator displays results in vertical format (one column per line) rather than tabular format. This is particularly useful for:
- Tables with many columns
- Columns containing long text values
- Debugging complex queries
- Comparing record structures
PostgreSQL offers the \x
command in psql to toggle expanded display mode, which provides similar functionality to MySQL's \G
:
-- Enable expanded display
\x auto
-- Run your query
SELECT * FROM users WHERE id = 1;
-- Alternatively, enable for single query
\x
SELECT * FROM products LIMIT 1;
\x
While both serve similar purposes, there are important distinctions:
- MySQL's
\G
affects only the current query - PostgreSQL's
\x
is a toggle that persists until changed \x auto
automatically switches to expanded mode only when needed
For more control over display formatting, consider these psql commands:
-- Set border style
\pset border 2
-- Adjust column display width
\pset format wrapped
\pset columns 80
-- Change null display representation
\pset null '(null)'
For programmatic access or when not using psql, you can:
-- Use JSON output
SELECT jsonb_pretty(to_jsonb(users)) FROM users LIMIT 1;
-- Format as aligned text table
SELECT * FROM table_name \gx
-- Use the expanded flag with \watch
\x auto
SELECT now() \watch 1