postgresql的psql常用命令-4

2023-01-07,,

psql是PostgreSQL的一个命令行交互式客户端工具

1. 查看postgresql账号
[root@localhost ~]#cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

2.登陆
[root@localhost ~]# su - postgres
-bash-4.1$ psql #连接到数据库服务器,可以在其中输入相应的SQL语句或者psql的命令,psql的命令都以\开始
psql (9.6.3)
Type "help" for help.

3.查看帮助
postgres=# \? #可以看到所有的psql的可执行命令,**请注意该命令显示的是psql客户端能够使用的命令**
General
\copyright 显示 PostgreSQL 的使用和发行条款
\errverbose 尽可能的显示所有当前的错误信息
\g [FILE] or ; 执行查询命令 (并且把结果输出到文件或者管道|)
\gexec 执行查询命令, 然后在结果中显示执行完成的所有的值
\gset [PREFIX] 执行查询命令并把结果存储在psql变量中
\q 退出psql
\crosstabview [COLUMNS] 执行插叙命令并把结果显示在交叉表中
\watch [SEC] 每SEC秒执行查询命令

Help
\? [commands] 显示反斜杠命令的帮助
\? options 显示在psql命令行选项的帮助
\? variables 显示某个变量的帮助
\h [NAME] SQL 命令语法上的说明,用 * 显示全部命令

Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\ev [VIEWNAME [LINE]] edit view definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE 将查询缓存区写入档案

Input/Output
\copy ... 执行 SQL COPY,数据流指向客户端主机
\echo [STRING] 将字符串写到标准输出流
\i FILE 从文件中执行命令
\ir FILE 与\i一样, 但是相对于当前脚本的位置
\o [FILE] 把所有查询结果输出到文件或者管道|
\qecho [STRING] 把字符串写到查询输出流 (see \o)

Informational
(options: S = show system objects, + = additional detail)
\d[S+] 显示所有的 tables, views, and sequences
\d[S+] NAME 显示指定名字的 table, view, sequence, or index 的表结构
\da[S] [PATTERN] 显示所有聚合函数
\dA[+] [PATTERN] 显示所有访问方法
\db[+] [PATTERN] 显示所有表空间
\dc[S+] [PATTERN] 显示字元编码转换
\dC[+] [PATTERN] 显示所有型别转换
\dd[S] [PATTERN] 显示所有物件的注解
\ddp [PATTERN] 显示所有默认权限
\dD[S+] [PATTERN] 显示所有共同值域
\det[+] [PATTERN] 显示所有外部表
\des[+] [PATTERN] 显示所有远程服务器
\deu[+] [PATTERN] 显示所有远程服务器用户
\dew[+] [PATTERN] 显示所有外部数据包装器
\df[antw][S+] [PATRN] 显示 [only agg/normal/trigger/window] 函数
\dF[+] [PATTERN] 显示所有文本搜索配置
\dFd[+] [PATTERN] 显示所有文本搜索字典
\dFp[+] [PATTERN] 显示所有文本搜索解析器
\dFt[+] [PATTERN] 显示所有文本搜索模板
\dg[S+] [PATTERN] 显示所有角色
\di[S+] [PATTERN] 显示所有索引
\dl 显示所有大对象, 与 \lo_list 类似
\dL[S+] [PATTERN] 显示所有程序语言
\dm[S+] [PATTERN] 显示所有具体视图
\dn[S+] [PATTERN] 显示所有模式
\do[S] [PATTERN] 显示所有操作符
\dO[S+] [PATTERN] 显示所有排序规则
\dp [PATTERN] 显示所有表 视图 序列 的访问权限
\drds [PATRN1 [PATRN2]] 显示每个数据库的角色设置
\ds[S+] [PATTERN] 显示所有序列
\dt[S+] [PATTERN] 显示所有表
\dT[S+] [PATTERN] 显示所有数据类型
\du[S+] [PATTERN] 显示所有角色
\dv[S+] [PATTERN] 显示视图
\dE[S+] [PATTERN] 显示所有外部表
\dx[+] [PATTERN] 显示所有扩展
\dy [PATTERN] 显示所有事件触发器
\l[+] [PATTERN] #列出所有的数据库
\sf[+] FUNCNAME 显示函数的定义
\sv[+] VIEWNAME 显示视图的定义
\z [PATTERN] 与\dp一样

Formatting
\a 切换不对齐和对齐的输出模式
\C [STRING] 设置表头,或者如果是none则不设置
\f [STRING] 显示或者设置未对齐输出的字段分隔符
\H 切换HTML输出模式(默认是off)
\pset [NAME [VALUE]] 设置表的输出选项
(NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|
numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager|
unicode_border_linestyle|unicode_column_linestyle|unicode_header_linestyle})
\t [on|off] 只显示行 (当前off)
\T [STRING] 设置HTML <table> 标签的属性, 或者如果是none则不设置
\x [on|off|auto] 切换扩展输出 (当前默认 off)

Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo} 连线到新的资料库 (目前是 "test")

\encoding [ENCODING] 显示或设定用户端字元编码
\password [USERNAME] 修改用户密码
\conninfo 显示当前的连接信息

Operating System
\cd [DIR] 改变目前的工作目录
\setenv NAME [VALUE] 设置或者取消环境变量
\timing [on|off] 切换命令计时开关 (默认是off)
\! [COMMAND] 执行shell命令 or 打开内部 shell命令

Variables
\prompt [TEXT] NAME 提示用户设置内部变量
\set [NAME [VALUE]] 设置内部变量, 假如没有参数则显示所有参数
\unset NAME 取消(删除)内部变量

Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID 大对象的操作

4.一些常用命令使用举例

(1)psql加上-E参数,可以把psql中各种以"\"开头的命令执行的实际SQL打印出来
-sh-4.1$ psql -E -h /var/opt/gitlab/postgresql -d gitlabhq_production
psql (9.2.18)
Type "help" for help.

gitlabhq_production=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' 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','v','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;

如果你在使用之后,想立即关闭
postgres=# \set ECHO_HIDDEN off
postgres=# \d
No relations found.

(2)显示所有表
gitlabhq_production=# \d
List of relations
Schema | Name | Type | Owner
--------+---------------------------------------------+----------+--------
public | abuse_reports | table | gitlab
public | abuse_reports_id_seq | sequence | gitlab

(3)\d后面跟一个表名,表示显示这个表的结构定义
gitlabhq_production=# \d abuse_reports
Table "public.abuse_reports"
Column | Type | Modifiers
--------------+-----------------------------+------------------------------------------------------------
id | integer | not null default nextval('abuse_reports_id_seq'::regclass)
reporter_id | integer |
user_id | integer |
message | text |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
message_html | text |
Indexes:
"abuse_reports_pkey" PRIMARY KEY, btree (id)

(4)\d后面可以跟一通配符"*"或"?"
gitlabhq_production=# \d a*
Table "public.abuse_reports"
Column | Type

| Modifiers
--------------+-----------------------------+------------------------------------------------------------
id | integer | not null default nextval('abuse_reports_id_seq'::regclass)
reporter_id | integer |
user_id | integer |
message | text |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
message_html | text |
Indexes:
"abuse_reports_pkey" PRIMARY KEY, btree (id)

(5)\d+命令,该命令将显示比\d命令更详细的信息
gitlabhq_production=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+---------------------------------------------+----------+--------+------------+-------------
public | abuse_reports | table | gitlab | 8192 bytes |
public | abuse_reports_id_seq | sequence | gitlab | 8192 bytes |
public | appearances | table | gitlab | 8192 bytes |

(6)显示所有表空间
gitlabhq_production=# \db
List of tablespaces
Name | Owner | Location
------------+-------------+----------
pg_default | gitlab-psql |
pg_global | gitlab-psql |
(2 rows)

(7)匹配不同对象类型的\d命令
只显示匹配的表,可以使用\dt命令
只显示索引,可以使用\di命令
只显示序号,可以使用\ds命令
只显示视图,可以使用\dv命令
只显示函数,可以使用\df命令

(8)想显示SQL已执行的时间,可以用\timing命令
gitlabhq_production=# \timing on
Timing is on.
sample_db=# select count(*) from abuse_reports;
count
-------
69
(69 row)

Time: 3.48 ms

(9)显示所有用户或者角色
gitlabhq_production=# \du
List of roles
Role name | Attributes | Member of
-------------------+------------------------------------------------+-----------
gitlab | | {}
gitlab-psql | Superuser, Create role, Create DB, Replication | {}
gitlab_replicator | Replication | {}

gitlabhq_production=# \dg
List of roles
Role name | Attributes | Member of
-------------------+------------------------------------------------+-----------
gitlab | | {}
gitlab-psql | Superuser, Create role, Create DB, Replication | {}
gitlab_replicator | Replication | {}

(10)\dp或\z命令用于显示表的权限分配情况
gitlabhq_production=# \dp abuse_reports
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+---------------+-------+-------------------+--------------------------
public | abuse_reports | table | |
(1 row)

gitlabhq_production=# \z abuse_reports
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+---------------+-------+-------------------+--------------------------
public | abuse_reports | table | |
(1 row)

(10)\x命令-可以把表中的每一行的每列数据都拆分为单行展示
gitlabhq_production=# \x
Expanded display is on.
gitlabhq_production=# \dp
Access privileges
-[ RECORD 1 ]------------+--------------------------------------------
Schema | public
Name | abuse_reports
Type | table
Access privileges |
Column access privileges |

(11)当客户端的字符编码和服务器的不一样时,可能会显示乱码,可以使用\encoding命令来指定客户端的字符编码,如使用\encoding utf8来指定客户端的编码方式为utf8

gitlabhq_production=# \encoding utf8

(12)\pset命令

\pset命令用于指定输出的格式,具体如下:

\pset border 0 : 表示输出内容物边框
\pset border 1 : 表示边框只在内部,默认情况下采用的是该条命令
\pset border 2 : 表示内外都存在边框

(13)\i <SQL文件的路径>
\i <SQL文件的路径>可以在pg中执行外部的SQL语句,这样方便我们执行很复杂的SQL语句。在MySQL中也存在类似的功能,但是实现的方式不一样,在MySQL中执行存储在外部文件中的SQL命令的方式:source <SQL文件的全路径> 或者 \. <SQL文件的全路径>

参考:
http://www.cnblogs.com/wangkangluo1/archive/2012/05/29/2523654.html
http://blog.csdn.net/jpzhu16/article/details/51931357

postgresql的psql常用命令-4的相关教程结束。

《postgresql的psql常用命令-4.doc》

下载本文的Word格式文档,以方便收藏与打印。