暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL特性矩阵解析系列28之Version aware psql

1080

描述

允许跨版本psql操作不同版本的 PostgreSQL Server。

psql — PostgreSQL的交互式终端。

psql是一个PostgreSQL的基于终端的前端。它让你能交互式地键入查询,把它们发送给PostgreSQL,并且查看查询结果。或者,输入可以来自于一个文件或者命令行参数。此外,psql还提供一些元命令和多种类似 shell 的特性来为编写脚本和自动化多种任务提供便利。

实验

    postgres@pg01-> psql -V
    psql (PostgreSQL) 12.8
    postgres@pg01-> psql -h 10.161.21.33 -p 5432 postgres postgres
    psql (12.8, server 10.12)
    Type "help" for help.
    postgres=#

    常用元命令

      postgres=# \l
      List of databases
      Name | Owner | Encoding | Collate | Ctype | Access privileges
      -----------+----------+----------+---------+-------+-----------------------
      postgres | postgres | UTF8 | C | C |
      template0 | postgres | UTF8 | C | C | =c/postgres +
      | | | | | postgres=CTc/postgres
      template1 | postgres | UTF8 | C | C | =c/postgres +
      | | | | | postgres=CTc/postgres
      (3 rows)

      postgres=# \dnS+
      List of schemas
      Name | Owner | Access privileges | Description
      --------------------+----------+----------------------+----------------------------------
      information_schema | postgres | postgres=UC/postgres+|
      | | =U/postgres |
      pg_catalog | postgres | postgres=UC/postgres+| system catalog schema
      | | =U/postgres |
      pg_temp_1 | postgres | |
      pg_toast | postgres | | reserved schema for TOAST tables
      pg_toast_temp_1 | postgres | |
      public | postgres | postgres=UC/postgres+| standard public schema
      | | =UC/postgres +|
      | | readonly=U/postgres |
      (6 rows)

      \? help,查看命令文档

      copy (select * from 表名) to '/tmp/1.csv';sql输出结果copy到一个文件中
      copy 表名 from '/tmp/1.csv'; csv文件中导入数据到表中

      \i FILE execute commands from file

      \c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
      connect to new database (currently "view_new")
                   
      \d+ 将显示比 \\d 更详细的信息,还会显示任何与表关系的注释,以及表中出现的 OID
      \dt 只显示匹配的表
      \di 只显示索引
      \ds  只显示序列
      \dv 只显示视图
      \df 只显示函数
      \timing on或off 显示 SQL 已执行的时间,默认情况下是 off

      \dn 列出所有的 schema
      \du 或 \\dg 列出所有的数据库用户和角色
      \db 显示所有的表空间,表空间其实是一个目录,放在这个表空间的表,就是把表的数据文件发到这个表空间下。
      \dp 或 \\z 显示表的权限分配情况
      \encoding 指定客户端的字符编码,如 \\encoding UTF8;
      \pset 设置输出的格式,\\pset border 0 : 表示输出内容无边框。border 1 :表示边框只在内部。border 2 :内外都有边框
      \x 把表中的每一行的每列数据都拆分为单行展示,与 MySQL 中的 "\\G" 的功能类似。

      文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论