在psql中垂直显示选择结果,就像MySQL的\ G一样

Dre*_*kes 62 postgresql psql

在MySQL中,您可以select使用\G(而不是\g)终止查询以垂直显示结果:

select * from foo \G

***************
 id: 1
bar: Hello
***************
 id: 2
bar: World
Run Code Online (Sandbox Code Playgroud)

如何使用psql为PostgreSQL做同样的事情?

Dre*_*kes 135

您可以通过启用展开显示来完成此操作.

通过切换此设置\x.例如:

# \x
Expanded display is on.
# \x
Expanded display is off.
Run Code Online (Sandbox Code Playgroud)

启用时,结果以表格(垂直)形式显示:

-[ RECORD 1 ]
id  | 1
bar | Hello
-[ RECORD 2 ]
id  | 2
bar | World
Run Code Online (Sandbox Code Playgroud)

您可以通过使用\x\g\x后缀切换展开的显示,运行查询,然后再将其关闭来为单个命令运行此命令.

select * from foo \x\g\x
Run Code Online (Sandbox Code Playgroud)

  • 或者在非交互式运行时使用`-x`开关(`psql db -xc'select*from foo'`). (3认同)