postgresql的psql经常使用命令-4

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

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/bashgit

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

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

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)bash

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)app

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.htmlhttp://blog.csdn.net/jpzhu16/article/details/51931357

相关文章
相关标签/搜索