134

In MySQL, you can terminate a select query with \G (as opposed to \g) to display the results vertically:

select * from foo \G

***************
 id: 1
bar: Hello
***************
 id: 2
bar: World

How can one do the same thing for PostgreSQL using psql?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 1
    See also http://serverfault.com/a/37260/131498 – phils Mar 18 '15 at 00:12
  • 1
    And also http://stackoverflow.com/questions/9604723/alternate-output-format-for-psql – pevik Oct 19 '15 at 17:04
  • Does this answer your question? [MySQL Extended Display](https://stackoverflow.com/questions/1794992/mysql-extended-display) – amphetamachine Oct 15 '20 at 19:11
  • 1
    @amphetamachine not really. That question is the inverse, so depending upon which DB you are familiar with, you'd come at this one or the other way. This question has many more views than that question, so I'd suggest that this resource has more SEO capital for whatever reason than the other answer as well. – Drew Noakes Oct 15 '20 at 23:39

2 Answers2

260

You can do this by enabling Expanded display.

Toggle this setting via \x. For example:

# \x
Expanded display is on.
# \x
Expanded display is off.

When on, results are shown in tabular (vertical) form:

-[ RECORD 1 ]
id  | 1
bar | Hello
-[ RECORD 2 ]
id  | 2
bar | World

You can run this for a single command by using the \x\g\x suffix to toggle expanded display on, run the query, then toggle it off again.

select * from foo \x\g\x

Or via psql param as shared here

psql db -xc 'select * from table'
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
1

Same can be achieved in dbeaver for postgres using "record" option in grid results

itsavy
  • 333
  • 4
  • 8