ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

postgres中psql使用和设置输出结果

2022-07-17 20:32:18  阅读:409  来源: 互联网

标签:psql 输出 27 San 1994 test Francisco 11 postgres


psql官方文档-12

如果在当前shell 下,如果设定 export PGPASSWORD='postgres密码' 环境变量,可以不用每次执行sql 语句或者导入一个sql 文件都输入一次密码的麻烦了。

1.设置输出结果边框

默认:\pset border 0

test=# select * from weather ;
     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

\pset border 0

test=# \pset border 2
Border style is 2.
test=# select * from weather ;
+---------------+---------+---------+------+------------+
|     city      | temp_lo | temp_hi | prcp |    date    |
+---------------+---------+---------+------+------------+
| San Francisco |      46 |      50 | 0.25 | 1994-11-27 |
| San Francisco |      33 |      60 | 0.15 | 1994-11-27 |
| San 66        |      66 |      45 | 0.15 | 1994-11-27 |
| San Francisco |      39 |      53 |    0 | 1994-11-29 |
| Hayward       |      33 |      50 |      | 1994-11-29 |
+---------------+---------+---------+------+------------+
(5 rows)

\pset border 0

test=# \pset border 0
Border style is 0.
test=# select * from weather ;
    city      temp_lo temp_hi prcp    date    
------------- ------- ------- ---- ----------
San Francisco      46      50 0.25 1994-11-27
San Francisco      33      60 0.15 1994-11-27
San 66             66      45 0.15 1994-11-27
San Francisco      39      53    0 1994-11-29
Hayward            33      50      1994-11-29
(5 rows)

2.输出结果到文件

客户端\o

不指定路径,默认输出到home家目录下

最后一个表示关闭文件输出

test=# \o test.txt
test=# select * from weather ;
test=# \o

终端-o

psql -U postgres -d test -o t.txt -c 'SELECT * FROM weather;'

3.控制表头

客户端\t

# 表头开启
test=# \t
Tuples only is off.
test=# select * from weather ;

     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

# 表头关闭
test=# \t
Tuples only is on.
test=# select * from weather ;

 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29

终端-t

~ psql -U postgres -d test -t -c 'SELECT * FROM weather;'
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29

~ psql -U postgres -d test -c 'SELECT * FROM weather;' 
     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

4.纵向横向显示

客户端\x

# 纵向输出
test=# \x
Expanded display is on.
test=# select * from weather where city='San 66';
-[ RECORD 1 ]-------
city    | San 66
temp_lo | 66
temp_hi | 45
prcp    | 0.15
date    | 1994-11-27

# 原始(横向输出)
test=# \x
Expanded display is off.
test=# select * from weather where city='San 66';
  city  | temp_lo | temp_hi | prcp |    date    
--------+---------+---------+------+------------
 San 66 |      66 |      45 | 0.15 | 1994-11-27
(1 row)

终端执行-x

psql -U postgres -d test -x -c "select * from weather where city='San 66'"

psql -U postgres -d test -c '\x' -c "select * from weather where city='San 66'"

5.对齐设置

对齐

客户端

test=# \pset format aligned
Output format is aligned.
test=# select * from weather;

     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

不对齐

客户端

test=# \pset format unaligned
Output format is unaligned.
test=# select * from weather;

city    temp_lo temp_hi prcp    date
San Francisco   46      50      0.25    1994-11-27
San Francisco   33      60      0.15    1994-11-27
San 66  66      45      0.15    1994-11-27
San Francisco   39      53      0       1994-11-29
Hayward 33      50              1994-11-29
(5 rows)

终端-A

psql -U postgres -d test -A -c 'SELECT * FROM weather;' 

city|temp_lo|temp_hi|prcp|date
San Francisco|46|50|0.25|1994-11-27
San Francisco|33|60|0.15|1994-11-27
San 66|66|45|0.15|1994-11-27
San Francisco|39|53|0|1994-11-29
Hayward|33|50||1994-11-29
(5 rows)

6.输出格式

csv格式

客户端

test=# \pset format csv
Output format is csv.
test=# select * from weather;
city,temp_lo,temp_hi,prcp,date
San Francisco,46,50,0.25,1994-11-27
San Francisco,33,60,0.15,1994-11-27
San 66,66,45,0.15,1994-11-27
San Francisco,39,53,0,1994-11-29
Hayward,33,50,,1994-11-29

终端--csv

psql --csv -U postgres -d test <<EOF
SELECT * FROM weather;
EOF
Password for user postgres: 
city,temp_lo,temp_hi,prcp,date
San Francisco,46,50,0.25,1994-11-27
San Francisco,33,60,0.15,1994-11-27
San 66,66,45,0.15,1994-11-27
San Francisco,39,53,0,1994-11-29
Hayward,33,50,,1994-11-29

html格式

客户端

test=# \pset format html
Output format is html.
test=# select * from weather;
<table border="1">
  <tr>
    <th align="center">city</th>
    <th align="center">temp_lo</th>
    <th align="center">temp_hi</th>
    <th align="center">prcp</th>
    <th align="center">date</th>
  </tr>
 ......
</table>
<p>(5 rows)<br />
</p>

终端-H

psql -U postgres -d test -H -c "select * from weather;"   

7.设置分隔符\pset fieldsep '|'

需要在\pset format unaligned下才能成功

客户端

test=# \pset fieldsep '//'
Field separator is "//".
test=# select * from weather;
city//temp_lo//temp_hi//prcp//date
San Francisco//46//50//0.25//1994-11-27
San Francisco//33//60//0.15//1994-11-27
San 66//66//45//0.15//1994-11-27
San Francisco//39//53//0//1994-11-29
Hayward//33//50////1994-11-29
(5 rows)

终端-F

~ psql -U postgres -d test -A -F '//' -c 'SELECT * FROM weather;'
city//temp_lo//temp_hi//prcp//date
San Francisco//46//50//0.25//1994-11-27
San Francisco//33//60//0.15//1994-11-27
San 66//66//45//0.15//1994-11-27
San Francisco//39//53//0//1994-11-29
Hayward//33//50////1994-11-29
(5 rows)

8.执行外部sql文件

\i

# test=# \i t.sql 默认找用户家目录下的文件
test=# \i /Users/lxd670/t.sql 
     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

终端执行sql文件-f

~ psql -U postgres -d test -f t.sql  
Password for user postgres: ******
     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

9.执行外部sql语句

终端执行sql语句EOF

psql -U postgres -d test <<EOF
SELECT * FROM weather;
EOF
Password for user postgres: ******
     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

终端执行sql语句-c

psql -U postgres -d test -c 'SELECT * FROM weather;'

     city      | temp_lo | temp_hi | prcp |    date    
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      33 |      60 | 0.15 | 1994-11-27
 San 66        |      66 |      45 | 0.15 | 1994-11-27
 San Francisco |      39 |      53 |    0 | 1994-11-29
 Hayward       |      33 |      50 |      | 1994-11-29
(5 rows)

标签:psql,输出,27,San,1994,test,Francisco,11,postgres
来源: https://www.cnblogs.com/lxd670/p/16488253.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有