如何查询PostgreSQL作为列名和值的列表而不是表

时间:2015-04-10 06:18:41

标签: postgresql psql

在MySQL中

我可以将查询结果显示为列表,例如:

   id : 123
 name : 'test'
 foo  : 'bar'

而不是标准结果:

+----+------+-----+
| id | name | foo |
+----+------+-----+
| 123|test  |bar  |
+----+------+-----+   

select * from tablename \G命令行中使用mysql

如何在PostgreSQL psql

中执行此操作

1 个答案:

答案 0 :(得分:1)

我认为您可能正在寻找扩展输出\x

regress=> \x
Expanded display is on.
regress=> select * from posts;
-[ RECORD 1 ]---------
id    | 1
title | bork bork bork
-[ RECORD 2 ]---------
id    | 2
title | honkey tonk

或者\x\t\a(扩展,仅元组,未对齐):

craig=> \x\t\a
Expanded display is on.
Tuples only is on.
Output format is unaligned.

craig=> select * from posts;
id|1
title|bork bork bork

id|2
title|honkey tonk

您也可能需要\f :

craig=> \x\t\a\f :
Expanded display is on.
Tuples only is on.
Output format is unaligned.
Field separator is ":".

craig=> select * from posts;
id:1
title:bork bork bork

id:2
title:honkey tonk

当我从脚本调用psql时,我倾向于使用:

psql -qAt

通常使用-0选项,因此它会将空字节作为记录分隔符发出,因此具有嵌入换行符的字段更容易处理。

相关问题