How to Use PostgreSQL Formatting Meta Commands with Real Examples and Output

When most developers open psql, they use it only for running SQL queries. But psql includes a powerful set of formatting meta commands that control how query results are displayed.

Are these commands grouped under the Formatting section in \? help output. They are useful when you need cleaner terminal output, CSV-style results, HTML reports, expanded row display, custom titles, or compact data views.

Open psql terminal and enter like this

postgres=# \?

You can see the meta commands related to the formatting like this

Formatting
  \a                     toggle between unaligned and aligned output mode
  \C [STRING]            set table title, or unset if none
  \f [STRING]            show or set field separator for unaligned query output
  \H                     toggle HTML output mode (currently off)
  \pset [NAME [VALUE]]   set table output option
                         (border|columns|csv_fieldsep|expanded|fieldsep|
                         fieldsep_zero|footer|format|linestyle|null|
                         numericlocale|pager|pager_min_lines|recordsep|
                         recordsep_zero|tableattr|title|tuples_only|
                         unicode_border_linestyle|unicode_column_linestyle|
                         unicode_header_linestyle|xheader_width)
  \t [on|off]            show only rows (currently off)
  \T [STRING]            set HTML <table> tag attributes, or unset if none
  \x [on|off|auto]       toggle expanded output (currently auto)

Create Sample Database and Table

First, create a database and connect to it.

create database test;
\c test;

Now create the sample table.

CREATE TABLE products (
    id INT,
    name TEXT,
    price NUMERIC,
    category TEXT
);

Insert sample rows.

INSERT INTO products VALUES
(1,'Laptop',55000,'Electronics'),
(2,'Mouse',800,'Accessories'),
(3,'Keyboard',1500,'Accessories');

Check the data.

select * from products;

Output:

id |   name   | price |  category
----+----------+-------+-------------
  1 | Laptop   | 55000 | Electronics
  2 | Mouse    |   800 | Accessories
  3 | Keyboard |  1500 | Accessories
(3 rows)

How to Use \a in psql

The \a command toggles between aligned and unaligned output.

Aligned output is the normal table format. Unaligned output is plain text separated by delimiters.

Switch to Unaligned Mode

test=# \a

Output:

Output format is unaligned.

Run query:

SELECT * FROM products;

Output:

id|name|price|category
1|Laptop|55000|Electronics
2|Mouse|800|Accessories
3|Keyboard|1500|Accessories
(3 rows)

Switch Back to Aligned Mode

test=# \a

Output:

Output format is aligned.

Run query again:

SELECT * FROM products;

Output:

id |   name   | price |  category
----+----------+-------+-------------
  1 | Laptop   | 55000 | Electronics
  2 | Mouse    |   800 | Accessories
  3 | Keyboard |  1500 | Accessories
(3 rows)

How to Use \C to Set a Title

The \C command adds a title above the query result.

\C 'Product List'
SELECT * FROM products;

Output:

           Product List
 id |   name   | price |  category
----+----------+-------+-------------
  1 | Laptop   | 55000 | Electronics
  2 | Mouse    |   800 | Accessories
  3 | Keyboard |  1500 | Accessories
(3 rows)

Remove Title

\C

Output:

Title is unset.

How to Use \f Field Separator

The \f command sets the separator used in unaligned mode.

Set Comma Separator

\f ,
\a
SELECT * FROM products;

Output:

id,name,price,category
1,Laptop,55000,Electronics
2,Mouse,800,Accessories
3,Keyboard,1500,Accessories
(3 rows)

Set Tab Separator

\f '	'
SELECT * FROM products;

Output:

id	name	price	category
1 Laptop 55000 Electronics
2 Mouse 800 Accessories
3 Keyboard 1500 Accessories
(3 rows)

How to Use \H HTML Output Mode

The \H command toggles HTML mode.

Enable HTML Output

\H
SELECT * FROM products;

Output:

<table border="1">
  <tr>
    <th align="center">id</th>
    <th align="center">name</th>
    <th align="center">price</th>
    <th align="center">category</th>
  </tr>
  <tr valign="top">
    <td align="right">1</td>
    <td align="left">Laptop</td>
    <td align="right">55000</td>
    <td align="left">Electronics</td>
  </tr>
  <tr valign="top">
    <td align="right">2</td>
    <td align="left">Mouse</td>
    <td align="right">800</td>
    <td align="left">Accessories</td>
  </tr>
  <tr valign="top">
    <td align="right">3</td>
    <td align="left">Keyboard</td>
    <td align="right">1500</td>
    <td align="left">Accessories</td>
  </tr>
</table>
<p>(3 rows)<br />
</p>
Disable HTML Output
\H

Output:

Output format is aligned.

How to Use \pset

The \pset command controls advanced output settings.

Change Border Style

\pset border 2
SELECT * FROM products;

Output:

+----+----------+-------+-------------+
| id |   name   | price |  category   |
+----+----------+-------+-------------+
|  1 | Laptop   | 55000 | Electronics |
|  2 | Mouse    |   800 | Accessories |
|  3 | Keyboard |  1500 | Accessories |
+----+----------+-------+-------------+
(3 rows)

Hide Footer

\pset footer off
SELECT * FROM products;

Output:

+----+----------+-------+-------------+
| id |   name   | price |  category   |
+----+----------+-------+-------------+
|  1 | Laptop   | 55000 | Electronics |
|  2 | Mouse    |   800 | Accessories |
|  3 | Keyboard |  1500 | Accessories |
+----+----------+-------+-------------+

Show Only Tuples

\pset tuples_only on
SELECT * FROM products;

Output:

|  1 | Laptop   | 55000 | Electronics |
|  2 | Mouse    |   800 | Accessories |
|  3 | Keyboard |  1500 | Accessories |
+----+----------+-------+-------------+

CSV Format

\pset format csv
SELECT * FROM products;

Output:

1,Laptop,55000,Electronics
2,Mouse,800,Accessories
3,Keyboard,1500,Accessories

Reset to Normal

\pset format aligned
\pset footer on
\pset tuples_only off

How to Use \t

The \t command is a shortcut for tuples only mode.

Enable

\t on
SELECT * FROM products;

Output:

|  1 | Laptop   | 55000 | Electronics |
|  2 | Mouse    |   800 | Accessories |
|  3 | Keyboard |  1500 | Accessories |
+----+----------+-------+-------------+

Disable

\t off
SELECT * FROM products;

Output:

+----+----------+-------+-------------+
| id |   name   | price |  category   |
+----+----------+-------+-------------+
|  1 | Laptop   | 55000 | Electronics |
|  2 | Mouse    |   800 | Accessories |
|  3 | Keyboard |  1500 | Accessories |
+----+----------+-------+-------------+
(3 rows)

How to Use \T HTML Table Attributes

The \T command sets attributes for HTML tables.

Change Border

\T border="3"
SELECT * FROM products;

Output begins with:

<table border="2" border="3">
  <tr>
    <th align="center">id</th>
    <th align="center">name</th>
    <th align="center">price</th>
    <th align="center">category</th>
  </tr>
  <tr valign="top">
    <td align="right">1</td>
    <td align="left">Laptop</td>
    <td align="right">55000</td>
    <td align="left">Electronics</td>
  </tr>
  <tr valign="top">
    <td align="right">2</td>
    <td align="left">Mouse</td>
    <td align="right">800</td>
    <td align="left">Accessories</td>
  </tr>
  <tr valign="top">
    <td align="right">3</td>
    <td align="left">Keyboard</td>
    <td align="right">1500</td>
    <td align="left">Accessories</td>
  </tr>
</table>
<p>(3 rows)<br />
</p>

Clear Attributes

\T

output:

Table attributes unset.

How to Use \x Expanded Display

The \x command is useful when rows are wide or have many columns.

Turn On Expanded Mode

\x
SELECT * FROM products;

Output:

+-[ RECORD 1 ]-----------+
| id       | 1           |
| name     | Laptop      |
| price    | 55000       |
| category | Electronics |
+-[ RECORD 2 ]-----------+
| id       | 2           |
| name     | Mouse       |
| price    | 800         |
| category | Accessories |
+-[ RECORD 3 ]-----------+
| id       | 3           |
| name     | Keyboard    |
| price    | 1500        |
| category | Accessories |
+----------+-------------+

Auto Mode

\x auto
SELECT * FROM products;

Output:

+----+----------+-------+-------------+
| id |   name   | price |  category   |
+----+----------+-------+-------------+
|  1 | Laptop   | 55000 | Electronics |
|  2 | Mouse    |   800 | Accessories |
|  3 | Keyboard |  1500 | Accessories |
+----+----------+-------+-------------+
(3 rows)

Formatting commands are not just cosmetic. They help in real work.

You can use them to:

  • Generate clean CSV output
  • Prepare HTML reports
  • Read wide rows easily
  • Remove extra row count messages
  • Change separators for scripts
  • Improve terminal readability
  • Customize output for automation

Many PostgreSQL users know SQL but ignore the formatting power built into psql. Once you learn commands like \a, \C, \f, \H, \pset, \t, \T, and \x, your terminal becomes far more useful.

These commands help you present data the way you need, whether you are debugging queries, exporting results, building reports, or simply making output easier to read.

If you use PostgreSQL daily, mastering these formatting meta commands is worth your time.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message