How to print output for each field in a row using the PostgreSQL Command

Posted on

Introduction

Actually, if there are too many fields or columns in a table after executing the query in a PostgreSQL Command Line, it will difficult to read. Actually, there is a solution to make the output is easy to read. That is by specifying the alternate output format to print as a result of the query execution. Normally, the result will be in a horizontal format. But in order to make it easy to read, changing the output format is also a better solution. Just change it so the result will be appear in a vertical format. Normally, the following output will be default format :

employee=# select * from output;
id | transaction_id | month | year | target | achievement | achievement_explanation | achievement_obstacle | plan | create_date | create_by | update_date | update_by
----+----------------+-------+-------+--------+-----------+-----------------------+-----------------------+-------------------------------------+----------+-------------------------------+---------------------------------+---------------------------------+-------------+-----------+-------------+-----------
1 | 249 | 1 | 2020 | | 20 | | | | | | | |
(1 row)


employee=#

Reformat the output of the Query Execution

The output of the above query execution is definitely harder to read. Just compare with the following output of the query command execution. But first of all, just activate a certain command for changing the output or display into an expanded mode. Type ‘\x on’ :

employee=# \x on
Expanded display is on.
employee=#

Finally, execute the query once more as follows :

employee=# select * from output;
-[ RECORD 1 ]-------------------+------------------------------------
id | 1
transaction_id | 249
month | 1
year | 2020
target |
achievement | 20
achievement_explanation | 
achievement_obstacles | 
plan | 
create_date |
create_by |
update_date |
update_by |
employee=#

It is clearly from the above query execution the output display is totally different. Rather than a display in the horizontal view, it present the vertical view. So, every field is appearing in a different row. The secret lies in the execution of the previous command. It is the query or the command ‘\x on’. Actually the ‘x’ is the abbreviation for expanded format. By giving the parameter ‘on’ for the ‘\x’ query command, it will activate the expanded format for every query command execution. For more information, just check in one of the StackOverFlow articles exist in this link.

Leave a Reply