5 Ways to Enable Vertical Output in psql

When we use psql to write SQL queries in PostgreSQL, we’ll usually see the results presented as a table, with columns displayed vertically and rows running horizontally. But it is possible to change this so that the columns are displayed horizontally and the rows run vertically.

This can be useful for when the number of columns causes the results to be wider than our psql display, and may cause the rows to wrap to the next line.

Here are five ways we can change the display to vertical output, otherwise known as expanded display.

The \x Command

The \x meta command can be used when we’re already connected to PostgreSQL via psql. This command allows us to toggle expanded display on and off. We can also specify either on, off, or auto to explicitly set it to a value.

Example:

\x

Result:

Expanded display is on.

So now when we run a query, we get the results in expanded display:

SELECT CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP;

Result:

current_date      | 2024-05-01
current_time | 15:31:32.623634+07
current_timestamp | 2024-05-01 15:31:32.623634+07
localtime | 15:31:32.623634
localtimestamp | 2024-05-01 15:31:32.623634

As expected, the columns are listed horizontally and the rows are listed vertically.

Let’s change it back:

\x

Result:

Expanded display is off.

Now that expanded display is off, our queries will be returned in the normal table like fashion where columns are vertical and rows are horizontal:

SELECT CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP;

Result:

 current_date |   current_time    |      current_timestamp       |   localtime    |      localtimestamp       
--------------+-------------------+------------------------------+----------------+---------------------------
2024-05-01 | 15:34:06.28439+07 | 2024-05-01 15:34:06.28439+07 | 15:34:06.28439 | 2024-05-01 15:34:06.28439

As mentioned, we also have the option of setting it to either on, off, or auto:

\x auto

Result:

Expanded display is used automatically.

The \pset x Command

We can also use the \pset command with the x option to toggle expanded display on and off:

\pset x

Result:

Expanded display is off.

We also have the option of explicitly specifying a value. If we do this, the value must be either on, off, or auto:

\pset x on

Result:

Expanded display is on.

The \pset expanded Command

Similar to the previous option, we can alternatively use the expanded option of the \pset command:

\pset expanded

Result:

Expanded display is off.

We also have the option of explicitly specifying a value. If we do this, the value must be either on, off, or auto:

\pset expanded auto

Result:

Expanded display is used automatically.

The -x Command Line Argument

We can also enable vertical output when we launch psql from the command line. We can do this with the -x option:

psql -x

That’s all that’s needed to enable expanded display when we launch psql. Any queries will automatically be displayed with vertical output.

Of course, we still have the ability to disable/toggle expanded display with the previously mentioned meta commands, such as \x, \pset x, and \pset expanded.

The --expanded Command Line Argument

Another way to enable vertical output from the command line is with the --expanded option:

psql --expanded

That does the same thing that -x does.

Again, we can disable or toggle expanded display by using the previously mentioned meta commands.