How to Change the Table Border Style in the psql Query Results

When using psql to run queries against PostgreSQL, we can use the \pset border and \pset linestyle commands to change the border of the table that displays our results.

Examples of \pset border

Here are some examples to demonstrate how the \pset border command affects the border format when using the default linestyle setting of ascii.

The value provided to \pset border must be a number. In general, the higher the number the more borders and lines the tables will have, but this depends on the particular format.

In HTML format, this translates directly into the border=… attribute.

In the other formats, while it’s possible to use higher numbers, only values 0 (no border), 1 (internal dividing lines), and 2 (table frame) make sense.

Border 0

\pset linestyle ascii
\pset border 0
SELECT * FROM Pets;

Result:

petid pettypeid ownerid petname    dob     
----- --------- ------- ------- ----------
    1         2       3 Fluffy  2020-11-20
    2         3       3 Fetch   2019-08-16
    3         2       2 Scratch 2018-10-01
    4         3       3 Wag     2020-03-15
    5         1       1 Tweet   2020-11-28
    6         3       4 Fluffy  2020-09-17
    7         3       2 Bark    
    8         2       4 Meow    
(8 rows)

Border 1

\pset linestyle ascii
\pset border 1
SELECT * FROM Pets;

Result:

 petid | pettypeid | ownerid | petname |    dob     
-------+-----------+---------+---------+------------
     1 |         2 |       3 | Fluffy  | 2020-11-20
     2 |         3 |       3 | Fetch   | 2019-08-16
     3 |         2 |       2 | Scratch | 2018-10-01
     4 |         3 |       3 | Wag     | 2020-03-15
     5 |         1 |       1 | Tweet   | 2020-11-28
     6 |         3 |       4 | Fluffy  | 2020-09-17
     7 |         3 |       2 | Bark    | 
     8 |         2 |       4 | Meow    | 
(8 rows)

Border 2

\pset linestyle ascii
\pset border 2
SELECT * FROM Pets;

Result:

+-------+-----------+---------+---------+------------+
| petid | pettypeid | ownerid | petname |    dob     |
+-------+-----------+---------+---------+------------+
|     1 |         2 |       3 | Fluffy  | 2020-11-20 |
|     2 |         3 |       3 | Fetch   | 2019-08-16 |
|     3 |         2 |       2 | Scratch | 2018-10-01 |
|     4 |         3 |       3 | Wag     | 2020-03-15 |
|     5 |         1 |       1 | Tweet   | 2020-11-28 |
|     6 |         3 |       4 | Fluffy  | 2020-09-17 |
|     7 |         3 |       2 | Bark    |            |
|     8 |         2 |       4 | Meow    |            |
+-------+-----------+---------+---------+------------+
(8 rows)

About \pset linestyle

We can use the \pset linestyle command to change the line drawing style of the border. It can be set to asciiold-ascii or unicode.

Example:

\pset linestyle ascii
\pset linestyle old-ascii
\pset linestyle unicode

Here’s a description of each:

  • ascii style uses plain ASCII characters. Newlines in data are shown using a + symbol in the right-hand margin. When the wrapped format wraps data from one line to the next without a newline character, a dot (.) is shown in the right-hand margin of the first line, and again in the left-hand margin of the following line.
  • old-ascii style uses plain ASCII characters, using the formatting style used in PostgreSQL 8.4 and earlier. Newlines in data are shown using a : symbol in place of the left-hand column separator. When the data is wrapped from one line to the next without a newline character, a ; symbol is used in place of the left-hand column separator.
  • unicode style uses Unicode box-drawing characters. Newlines in data are shown using a carriage return symbol in the right-hand margin. When the data is wrapped from one line to the next without a newline character, an ellipsis symbol is shown in the right-hand margin of the first line, and again in the left-hand margin of the following line.

If you need more border formatting options, check out the pretty borders in psql patch.