墨墨导读:本文主要详述PostgreSQL的常见参数以及一些技巧。


1. psql命令


1.1 General options


1.1.1- ?


我们可以psql -?或者psql --help看下psql有哪些可用参数:

[postgres@host01 ~]$ psql --help
psql is the PostgreSQL interactive terminal.
Usage:  psql [OPTION]... [DBNAME [USERNAME]]
General options:    -c, --command=COMMAND    run only single command (SQL or internal) and exit    -d, --dbname=DBNAME      database name to connect to (default: "postgres")   -f, --file=FILENAME      execute commands from file, then exit -l, --list               list available databases, then exit    -v, --set=, --variable=NAME=VALUE    set psql variable NAME to VALUE (e.g., -v ON_ERROR_STOP=1) -V, --version            output version information, then exit  -X, --no-psqlrc          do not read startup file (~/.psqlrc)   -1 ("one"), --single-transaction  execute as a single transaction (if non-interactive)    -?, --help[=options]     show this help, then exit --help=commands      list backslash commands, then exit    --help=variables     list special variables, then exit
Input and output options:   -a, --echo-all           echo all input from script -b, --echo-errors        echo failed commands   -e, --echo-queries       echo commands sent to server   -E, --echo-hidden        display queries that internal commands generate    -L, --log-file=FILENAME  send session log to file  -n, --no-readline        disable enhanced command line editing (readline)   -o, --output=FILENAME    send query results to file (or |pipe) -q, --quiet              run quietly (no messages, only query output)   -s, --single-step        single-step mode (confirm each query)  -S, --single-line        single-line mode (end of line terminates SQL command)
Output format options:  -A, --no-align           unaligned table output mode    -F, --field-separator=STRING   field separator for unaligned output (default: "|")   -H, --html               HTML table output mode -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \pset command)   -R, --record-separator=STRING  record separator for unaligned output (default: newline)    -t, --tuples-only        print rows only    -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)   -x, --expanded           turn on expanded table output  -z, --field-separator-zero  set field separator for unaligned output to zero byte   -0, --record-separator-zero set record separator for unaligned output to zero byte
Connection options: -h, --host=HOSTNAME      database server host or socket directory (default: "local socket")  -p, --port=PORT          database server port (default: "5432")  -U, --username=USERNAME  database user name (default: "postgres")    -w, --no-password        never prompt for password  -W, --password           force password prompt (should happen automatically)
For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.  Report bugs to <pgsql-bugs@postgresql.org>.

1.1.2- E


我们可以实验一下-E的效果:

[postgres@host01 ~]$ psql -E
psql (11.2)
Type "help" for help. postgres=# \c yx
You are now connected to database "yx" as user "postgres".
yx=# \d
********* QUERY **********
SELECT n.nspname as "Schema", c.relname as "Name",  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' WHEN 'I' THEN 'index' END as "Type",   pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c  LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','v','m','S','f','')   AND n.nspname <> 'pg_catalog'   AND n.nspname <> 'information_schema'   AND n.nspname !~ '^pg_toast'  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************  List of relations   Schema |  Name   | Type  |  Owner
--------+---------+-------+----------    public | student | table | postgres public | yx      | table | postgres
(2 rows)

我们看到多出来一条sql,那么这条sql就是pg为我们展示\d的结果所使用的sql。


1.1.3- c

-c就是不用进psql,直接执行命令:

[postgres@host01 ~]$ psql yx -c "select * from student;"  id |               name               | number
----+----------------------------------+--------  1 | aaa                              | 1023
(1 row)

但是我们看哈,如果连着俩条sql什么效果:

[postgres@host01 ~]$ psql yx -c "select * from student;select count(1) from student;"   count
------- 1
(1 row)

我们看到,只显示了最后一条sql的执行结果。

但是前面的sql到底执行了么?肯定执行了,前面的sql语法错了要报错。我们看下面一个例子:

[postgres@host01 ~]$ psql yx -c "insert into student values(2,'bbbb',1024);insert into student values(3,'cccc',1025);select * from student;"  id |               name               | number
----+----------------------------------+--------  1 | aaa                              | 1023     2 | bbbb                             | 1024     3 | cccc                             | 1025
(3 rows)

那么我们要想显示一批sql的执行结果咋办呢?一条命令指定一个-c:

[postgres@host01 ~]$ psql yx -c "select * from student;" -c "select count(1) from student;"  id |               name               | number
----+----------------------------------+--------  1 | aaa                              | 1023     2 | bbbb                             | 1024     3 | cccc                             | 1025
(3 rows)    count
------- 3
(1 row)

那么为了方便,还可以使用下面的-f。

1.1.4- f

我们把刚才要执行的sql写到一个文件中:

[postgres@host01 ~]$ cat test.sql
select * from student;
select count(1) from student;

然后用-f来执行这个文件:

[postgres@host01 ~]$ psql yx -f test.sql   id |               name               | number
----+----------------------------------+--------  1 | aaa                              | 1023     2 | bbbb                             | 1024     3 | cccc                             | 1025
(3 rows)    count
------- 3
(1 row)

那我想实现动态sql,咋办,就是-v。

1.1.5- v

我们把test.sql改一下:

[postgres@host01 ~]$ cat test.sql
select * from student where id=:1;
select * from student where id=:b;

然后我们使用-v来给绑定变量赋值:

[postgres@host01 ~]$ psql yx -f test.sql -v 1=2 -v b=1 id |               name               | number
----+----------------------------------+--------  2 | bbbb                             | 1024
(1 row) id |               name               | number
----+----------------------------------+--------  1 | aaa                              | 1023
(1 row)

我们知道,PLSQL里的动态sql,表名和列名是不能作为绑定变量。需要在动态sql中将表名、列名做为字符串变量,来拼接sql。

psql结合-f -v来实现动态sql就不一样了,我们改写test.sql如下:

[postgres@host01 ~]$ cat test.sql
select * from :tab1 where id=2;
select * from :tab2 where id=1;

然后我们执行来看效果:

[postgres@host01 ~]$ psql yx -f test.sql -v tab1=student -v tab2=t1 id |               name               | number
----+----------------------------------+--------  2 | bbbb                             | 1024
(1 row) id | name
----+------    1 | yx
(1 row)

所以说,psql这个并不是真正的绑定变量传值,而是跟拼接字符串一个道理。


1.1.6- d -l -V

-V可以看psql的版本号,-l就是列出可用的database name,-d 就是直接连到某个database中:

[postgres@host01 ~]$ psql -V
psql (PostgreSQL) 11.2  [postgres@host01 ~]$ psql -l
Password for user postgres:     List of databases   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------  postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |   template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +  |          |          |             |             | postgres=CTc/postgres  template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +  |          |          |             |             | postgres=CTc/postgres  yx        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)    [postgres@host01 ~]$ psql -d yx
Password for user postgres:
psql (11.2)
Type "help" for help. yx=# select current_database();    current_database
------------------  yx
(1 row)


1.2 Connection options

Connection options:   -h, --host=HOSTNAME      database server host or socket directory (default: "local socket")  -p, --port=PORT          database server port (default: "5432")  -U, --username=USERNAME  database user name (default: "postgres")    -w, --no-password        never prompt for password  -W, --password           force password prompt (should happen automatically)

想要用到这些,需要修改如下文件:

[postgres@host01 ~]$ ls $PGDATA/pg_hba.conf
/pgdata/pg_hba.conf

约等于配置黑白名单,以及访问方式,文件主要内容如下:

# TYPE  DATABASE        USER            ADDRESS                 METHOD   # "local" is for Unix domain socket connections only
local   all             all                                     password
#local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             192.168.12.10/24        trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

method可以控制访问方式:

# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
# "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert".
# Note that "password" sends passwords in clear text; "md5" or
# "scram-sha-256" are preferred since they send encrypted passwords.

修改完该文件记得重载才能生效:

[postgres@host01 ~]$ pg_ctl reload
server signaled


1.3 Output format options

这个还是有点儿意思,比如-H就是用html的格式输出结果:

扩展阅读


  1. 《PostgreSQL 指南:内幕探索》之基础备份与时间点恢复(上)

  2. 《PostgreSQL 指南:内幕探索》之基础备份与时间点恢复(下)

  3. 解读年度数据库PostgreSQL:如何处理并发控制(一)

  4. “王者对战”之 MySQL 8 vs PostgreSQL 10

  5. 大象起舞:用PostgreSQL解海盗分金问题

  6. 解读年度数据库PostgreSQL:如何巧妙地实现缓冲区管理器

  7. 解读年度数据库PostgreSQL:如何处理并发控制(一)

  8. 数据和云,半年文章精选(文末赠书)

  9. 美女DBA带你了解PostgreSQL用户及角色

  10. 从Oracle到PostgreSQL:最全控制文件

数据和云

ID:OraNews

如有收获,请划至底部,点击“在看”,谢谢!

资源下载

关注公众号:数据和云(OraNews)回复关键字获取

help,30万+下载的完整菜单栏

2019DTCC,数据库大会PPT

2018DTCC , 数据库大会PPT

2018DTC,2018 DTC 大会 PPT

ENMOBK,《Oracle性能优化与诊断案例》

DBALIFE,“DBA 的一天”海报

DBA04,DBA 手记4 电子书

122ARCH,Oracle 12.2体系结构图

2018OOW,Oracle OpenWorld 资料

产品推荐

云和恩墨Bethune Pro2 企业版,集监控、巡检、安全于一身,你的专属数据库实时监控和智能巡检平台,漂亮的不像实力派,你值得拥有!

云和恩墨zData一体机现已发布超融合版本和精简版,支持各种简化场景部署,零数据丢失备份一体机ZDBM也已发布,欢迎关注。

云和恩墨大讲堂 | 一个分享交流的地方

长按,识别二维码,加入万人交流社群

请备注:云和恩墨大讲堂

PostgreSQL的常见参数和技巧相关推荐

  1. Perl命令行常见用法及技巧

    Perl命令行常见用法及技巧 作者:懒人运维 来源: 懒人运维   替换 将所有C程序中的foo替换成bar,旧文件备份成.bak perl -p -i.bak -e 's/\bfoo\b/bar/g ...

  2. Vue开发中的一些常见套路和技巧(上)

    Vue开发中的一些常见套路和技巧(上) 简介 大家好呀,我是 wangly19 ,这次文章主要是来总结下我在使用 Vue.js 总结出来的一些套路,可以做一些查缺补漏.如果还有有趣的小技巧,也可以在评 ...

  3. Postgresql服务器配置-设置参数

    Postgresql服务配置-设置参数 1.Parameter Names and Values 每个参数都有一个值.所有参数名称都不区分大小写.每个参数值都采用五种类型之一: 布尔.字符串.整数.浮 ...

  4. nginx 常见参数以及重定向参数配置

    nginx 常见参数以及重定向参数配置 nginx 各参数翻译,作用 $arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值. $args #这个变量等于请求行中 ...

  5. JVM的内存结构,Eden和Survivor比例;JVM中一次完整的GC流程,对象如何晋升到老年代,说说你知道的几种主要的JVM参数;CMS 常见参数解析;.你知道哪几种垃圾收集器,各自的优缺点

    47.JVM的内存结构,Eden和Survivor比例 49.JVM中一次完整的GC流程是怎样的,对象如何晋升到老年代,说说你知道的几种主要的JVM参数 50.-XX:+CMSScavengeBefo ...

  6. 垃圾收集器与内存分配策略(五)之垃圾日志与常见参数

    2019独角兽企业重金招聘Python工程师标准>>> 垃圾收集器与内存分配策略(五)--垃圾日志与常见参数 理解GC日志 每个收集器的日志格式都可以不一样,但各个每个收集器的日志都 ...

  7. vue 后台获取数据 下拉框_Vue开发中的一些常见套路和技巧

    属性排放 管理请求加载状态 Proxy跨域 对developer和build的打包进行不同配置 大部分开发者都喜欢将Vue的config写在一个文件中,看起来是没有问题,但是随着环境的变化,项目优化, ...

  8. JSR303常见参数

    常见参数 @NotNull(message = "名字不能为空")private String userName;@Max(value = 120, message = " ...

  9. Java虚拟机--------JVM常见参数

    JVM系列常见参数及学习思路: JVM 调优常见参数 12345678910111213141516 Java1.7的jvm参数查看一下官方网站.http://docs.oracle.com/java ...

最新文章

  1. C++拾取——使用stl标准库生成等差、等比数列的方法
  2. Tengine 一个注重性能和兼容性的AI框架
  3. mysql read file_MySQL利用OS读写文件的前提
  4. Music Notes(前缀和+二分)
  5. 控制反转-依赖倒置-依赖注入
  6. Duilib学习笔记《03》— 控件使用
  7. Excel中这四个常出错的地方,你一定中过!
  8. 官宣!.NET官网发布中⽂版
  9. 【自适应盲均衡7】分数间隔的复数常模算法(FSE-CMA)
  10. mysql复合语句声明开始于_mysql8 参考手册--BEGIN ... END复合语句
  11. git安装 perl ubuntu_Ubuntu系统上安装Git
  12. high performance web sites 阅读小记
  13. JAVA清稿word_java开发实现word在线编辑及流转
  14. [转载]Spring Cloud微服务Sentinel+Apollo限流、熔断实战
  15. Mac电脑如何让聚焦功能显示在菜单栏?
  16. 【0x50 动态规划】Mobile Service【线性DP】
  17. python爬虫淘宝评论图片_淘宝上的图片是怎么被爬取的
  18. MES系统架构初版ZXW
  19. 在Storyboard中设置borderColor
  20. SpringMvc导入Excel

热门文章

  1. cassandra 数据量_Cassandra和Spark的数据处理简介
  2. 如何击败腾讯_要击败这个新的电子游戏,请对其重新编程
  3. network/request.js网络请求模块封装
  4. LeetCode 451. 根据字符出现频率排序(Sort Characters By Frequency)
  5. java关于excel的导出_[转载]关于JAVA导出Excel
  6. ROS笔记(37) 抓取和放置
  7. 深度学习笔记(34) 目标检测
  8. Python笔记(1) Python简介
  9. map std 浮点数索引_C std :: map持有任何类型的值
  10. WPF中退出时显示是否保存数据提示