首页 > 其他分享 >格式化输出\pset命令详解

格式化输出\pset命令详解

时间:2023-08-01 15:33:03浏览次数:46  
标签:5432 02 格式化 postgres linuxpg51 详解 pset 7698


  • 语法
    "\pset"命令的语法如下:
\pset [option [value] ]

postgres@linuxpg51:5432=#\pset
border                   1
columns                  0
csv_fieldsep             ','
expanded                 off
fieldsep                 '|'
fieldsep_zero            off
footer                   on
format                   aligned
linestyle                ascii
null                     ''
numericlocale            off
pager                    1
pager_min_lines          0
recordsep                '\n'
recordsep_zero           off
tableattr                
title                    
tuples_only              off
unicode_border_linestyle single
unicode_column_linestyle single
unicode_header_linestyle single

根据命令后面“option”和“value”的不同可以设置很多种不同的输出格式,这里只介绍一些常用的用法。

  • 默认情况下,psql中执行SQL语句后输出的内容是只有内边框的表格:
postgres@linuxpg51:5432=#select * from emp;
 empno | ename  |    job    | mgr  |  hiredate  |   sal   |  comm   | deptno 
-------+--------+-----------+------+------------+---------+---------+--------
  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |         |     20
  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30
  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30
(3 rows)

postgres@linuxpg51:5432=#
  • 如果要像MySQL中一样输出带有内外边框的表格内容,可以用命令"\pset boder 2"来实现,示例如下:
postgres@linuxpg51:5432=#\pset border 2
Border style is 2.

postgres@linuxpg51:5432=#select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  |    job    | mgr  |  hiredate  |   sal   |  comm   | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |         |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
+-------+--------+-----------+------+------------+---------+---------+--------+
(3 rows)
postgres@linuxpg51:5432=#
  • 当然也可以用"\pset boder 0"命令输出不带任何边框的内容,示例如下:
postgres@linuxpg51:5432=#\pset border 0
Border style is 0.
postgres@linuxpg51:5432=#select * from emp;
empno ename     job    mgr   hiredate    sal    comm   deptno 
----- ------ --------- ---- ---------- ------- ------- ------
 7369 SMITH  CLERK     7902 1980-12-17  800.00             20
 7499 ALLEN  SALESMAN  7698 1981-02-20 1600.00  300.00     30
 7521 WARD   SALESMAN  7698 1981-02-22 1250.00  500.00     30
(3 rows)

postgres@linuxpg51:5432=#

综上所述,“\pset”命令设置边框的用法如下:

·\pset border 0:表示输出内容无边框。
·\pset border 1:表示输出内容只有内边框。
·\pset border 2:表示输出内容内外都有边框。
psql中默认的输出格式是"\pset border 1"。
  • 不管输出的内容加不加边框,内容本身都是对齐的,是为增强数据的可读性而专门格式化过的,而有时我们需要把命令的结果输出为其他程序可以读取的文件,如以逗号分隔或以Tab分隔的文本文件,这时就需要用到"\pset format unaligned"命令了,示例如下:
postgres@linuxpg51:5432=#\pset format unaligned
Output format is unaligned.
postgres@linuxpg51:5432=#select * from emp;
empno|ename|job|mgr|hiredate|sal|comm|deptno
7369|SMITH|CLERK|7902|1980-12-17|800.00||20
7499|ALLEN|SALESMAN|7698|1981-02-20|1600.00|300.00|30
7521|WARD|SALESMAN|7698|1981-02-22|1250.00|500.00|30
(3 rows)
postgres@linuxpg51:5432=#
  • 默认分隔符是"|",我们可以用命令"\pset fieldsep"来设置分隔符
改成Tab分隔符的方法如下:
\pset fieldsep '\t'
postgres@linuxpg51:5432=#\pset format unaligned
Output format is unaligned.
postgres@linuxpg51:5432=#\pset fieldsep '\t'
Field separator is "    ".
postgres@linuxpg51:5432=#select empno,ename from emp;
empno   ename
7369    SMITH
7499    ALLEN
7521    WARD
(3 rows)

改成","分隔符的方法如下:
postgres@linuxpg51:5432=#\pset fieldsep ','
Field separator is ",".

postgres@linuxpg51:5432=#select * from emp;
empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,SMITH,CLERK,7902,1980-12-17,800.00,,20
7499,ALLEN,SALESMAN,7698,1981-02-20,1600.00,300.00,30
7521,WARD,SALESMAN,7698,1981-02-22,1250.00,500.00,30
(3 rows)

标签:5432,02,格式化,postgres,linuxpg51,详解,pset,7698
From: https://blog.51cto.com/u_11585528/6922859

相关文章

  • js set和map详解
      当我们需要存储唯一值的集合时,可以使用Set。Set是一种有序的、无重复值的集合,它可以存储任何类型的值,包括原始值和对象。下面是使用Set的示例:Copy//创建一个SetconstmySet=newSet();//添加值mySet.add(1);mySet.add(2);mySet.add(3);//删除值......
  • js weekset和weekmap详解
    在JavaScript中,WeakSet和WeakMap是Set和Map的变体,它们的特点是只能存储对对象的弱引用。这意味着,如果一个对象只被WeakSet或WeakMap引用,而没有被其他地方引用,那么它可能会被垃圾回收器清理掉。这在某些情况下非常有用,比如处理缓存或临时数据等。WeakSet和WeakMap的用法与Set和Map......
  • SaaS营销策略详解:透过客户激励做好数字营销,做好口碑营销
    当前很多SaaS厂商为了推广促销,使得市面上的SaaS营销策略五花八门,不同的营销策略所带来的效果则各有优劣。SaaS营销策略一个好的SaaS营销策略最好的成效是四两拨千斤,在数字营销的框架内,传统的口碑营销则重新焕发活力,通过客户激励的形式来做好数字营已经成为了当下SaaS营销策略中最重......
  • DecimalFormat格式化数据.00问题
    废话不多说,直接上代码publicvoiddecimalFormat(){DecimalFormatdf=newDecimalFormat("#.00");Stringformat=df.format(Double.valueOf("9.00"));System.out.println(format);}优化方式publicvoiddecimalFormat(){DecimalFormatdf......
  • libgdx——FileHanle详解
    FilehandlingTableofContentsanotefromthetranslationWikiStyleGuideDeveloper'sGuideIntroductionGoals&FeaturesCommunity&SupportContributingGamesBuiltwithLibgdxPrerequisitesGradleProjectSetup,Running,DebuggingandPa......
  • SpringBoot进行参数校验的方法详解
    https://www.jb51.net/article/246275.htm在日常的接口开发中,为了防止非法参数对业务造成影响,经常需要对接口的参数进行校验。本文通过示例详细讲解了SpringBoot如何进行参数校验的,感兴趣的可以学习一下 +目录介绍在日常的接口开发中,为了防止非法参数对业务造成影响,经常......
  • Linux 6.6+ Oracle RAC 12c搭建详解
    1. RedHatEnterpriseLinuxServerrelease6.6x86_64两台2. Oracle12.1.0.13. ASM存储方式4. 软件下载:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/oracle12c-linux-12201-3608234.html 5. 基础安装包yum-yinstallbinutils.x86......
  • Mybatis学习(1)——mybatis介绍 & 入门案例 & 全局配置文件详解 & 增删改查 + mybatis事
    Mybatis学习(1)——mybatis介绍&入门案例&全局配置文件详解&增删改查+mybatis事务&mapper.xml文件#{}和${}&动态SQL入门原文链接:https://blog.csdn.net/Pireley/article/details/131520252目录引出一、mybatis是啥1.官网&ORM(ObjectRelationMapping)对象关......
  • js日期格式化的两种方法
    1.当我们想先获取当前的时间时,这是第一种方法特别简单:返回格式 ThuJul28202215:04:32GMT+0800(中国标准时间)newdateNow=newDate();//ThuJul28202215:04:32GMT+0800(中国标准时间)科普下时间的时分秒及星期:date.getYear();//获取当......
  • Java中面向对象详解
    一.定义面向对象是:将事务高度抽象化的编程模式将问题分解成一个个小步骤,对每个步骤进行抽象,形成对象,通过不同的对象之间调用,组合解决问题。在进行面向对象进行编程时,要把属性、行为等封装成对象,然后基于这些对象及对象的能力进行业务逻辑的实现。创建一次,重复使用二.面向......