代码改变世界

psql元命令使用说明

2019-07-22 16:55  TankDBA  阅读(857)  评论(0编辑  收藏  举报

版本说明:

           PostgresQL 10.9


psql提供了丰富的元命令,以“\”开头,使用元命令可以高效、便捷的对数据库进行管理。psql命令的格式是用反斜线后面直接跟上一个命令动词,然后是一些参数。

在使用前可以使用“\?”来获取帮助信息,了解可以使用的命令清单。

$ psql

psql (10.9)

Type "help" for help.


postgres=# \?

由于篇幅限制,输出的内容不在这里描述,以下内容将介绍几种常用的元命令。

1 查看当前数据库的连接信息(\conninfo)

$ psql
psql (10.9)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
postgres=# 

2 修改当前工作路径(\cd [directory])

如果不带参数,则切换到当前用户的主目录。\! pwd用来显示当前工作路径(叹号后有空格)。

postgres=# \! pwd
/home/postgres
postgres=# \!pwd
Invalid command \!pwd. Try \? for help.
postgres=# \cd /pgtbs
postgres=# \! pwd
/pgtbs
postgres=# 

3 查看表、视图和序列的访问权限(\dp)

列出表、视图和序列,包括与它们相关的访问特权。

$ psql mydb pguser
psql (10.9)
Type "help" for help.

mydb=> \dp
                            Access privileges
 Schema | Name | Type  | Access privileges | Column privileges | Policies 
--------+------+-------+-------------------+-------------------+----------
 public | t1   | table |                   |                   | 
(1 row)

mydb=> 

4 查看已定义的配置信息(\drds)

列出已定义的配置设置。这些设置可以是针对角色的、针对数据库的或者同时针对两者的。

mydb=> \drds
Did not find any settings.
mydb=> 

5 查看数据库角色信息(\du)

mydb=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 pguser    |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

mydb=> 

6 查看字符集编码(\encoding)

mydb=> \encoding
UTF8
mydb=>

7 查看最近发生的错误信息(\errverbose)

将以最详细程度展示出最近的服务器错误消息。

mydb=> select * from t2;
ERROR:  relation "t2" does not exist
LINE 1: select * from t2;
                      ^
mydb=> 
mydb=> \errverbose
ERROR:  42P01: relation "t2" does not exist
LINE 1: select * from t2;
                      ^
LOCATION:  parserOpenTable, parse_relation.c:1180
mydb=> 

8 显示执行时间(\timing on | off)

mydb=> \timing on
Timing is on.
mydb=> select id,name from t1 limit 10;
 id |  name   
----+---------
  1 | 1_tank
  2 | 2_tank
  3 | 3_tank
  4 | 4_tank
  5 | 5_tank
  6 | 6_tank
  7 | 7_tank
  8 | 8_tank
  9 | 9_tank
 10 | 10_tank
(10 rows)

Time: 0.698 ms
mydb=> 

9 转义到shell(\! [command])

有参数时,执行shell命令;

无参数时,转义到子shell;当子shell退出时,psql会恢复。

mydb=> \! pwd
/pgtbs
mydb=> \!
[postgres@tank pgtbs]$ pwd
/pgtbs
[postgres@tank pgtbs]$ exit
exit
mydb=>

10 查看数据库列表(\l)

\符号后面为小写的“L”

mydb=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 mydb      | pguser   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/pguser           +
           |          |          |             |             | pguser=C*T*c*/pguser
 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
(4 rows)

11 查看表空间列表(\db)

mydb=> \db
            List of tablespaces
    Name    |  Owner   |      Location      
------------+----------+--------------------
 pg_default | postgres | 
 pg_global  | postgres | 
 tbs_mydb   | pguser   | /pgtbs/10/tbs_mydb
(3 rows)

12 查看表定义(\d table_name

--创建测试表
$ psql mydb pguser

mydb=> create table t1(id int4,name text,create_time timestamp without time zone default clock_timestamp());

CREATE TABLE

mydb=> alter table t1 add primary key(id);

ALTER TABLE
mydb=> \d t1
                                  Table "public.t1"
   Column    |            Type             | Collation | Nullable |      Default      
-------------+-----------------------------+-----------+----------+-------------------
 id          | integer                     |           | not null | 
 name        | text                        |           |          | 
 create_time | timestamp without time zone |           |          | clock_timestamp()
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)

mydb=>

13 查看表占用空间大小(\dt+table_name

--为测试表插入50万条数据

$ psql mydb pguser

mydb=> insert into t1(id,name) select n,n || '_tank' from generate_series(1,500000) n;

INSERT 0 500000

mydb=> \dt+t1
                  List of relations
 Schema | Name | Type  | Owner  | Size  | Description 
--------+------+-------+--------+-------+-------------
 public | t1   | table | pguser | 25 MB | 
(1 row)

mydb=> 

14 查看索引占用空间大小(\di+table_name&index_name

mydb=> \di+t1
                        List of relations
 Schema |  Name   | Type  | Owner  | Table | Size  | Description 
--------+---------+-------+--------+-------+-------+-------------
 public | t1      | table | pguser |       | 25 MB | 
 public | t1_pkey | index | pguser | t1    | 11 MB | 
(2 rows)

mydb=> \di+t1_key
                        List of relations
 Schema |  Name   | Type  | Owner  | Table | Size  | Description 
--------+---------+-------+--------+-------+-------+-------------
 public | t1      | table | pguser |       | 25 MB | 
 public | t1_pkey | index | pguser | t1    | 11 MB | 
(2 rows)

mydb=> 

15 设置标题(\C title_name)

mydb=> \C "Here is my list:"
Title is ""Here is my list:"".
mydb=> select id,name from t1 limit 10;
"Here is my list:"
 id |  name   
----+---------
  1 | 1_tank
  2 | 2_tank
  3 | 3_tank
  4 | 4_tank
  5 | 5_tank
  6 | 6_tank
  7 | 7_tank
  8 | 8_tank
  9 | 9_tank
 10 | 10_tank
(10 rows)

mydb=>

16 执行当前查询缓冲区(\g)

如果当前查询缓冲区为空,则最新发送的查询将被重新执行。

mydb=> \g
"Here is my list:"
 id |  name   
----+---------
  1 | 1_tank
  2 | 2_tank
  3 | 3_tank
  4 | 4_tank
  5 | 5_tank
  6 | 6_tank
  7 | 7_tank
  8 | 8_tank
  9 | 9_tank
 10 | 10_tank
(10 rows)

mydb=> 

17 将当前缓冲区打印到标准输出(\p)

mydb=> \p
select id,name from t1 limit 10;
mydb=> \print
select id,name from t1 limit 10;
mydb=>

18 查看函数代码(\sf function_name

mydb=> \sf random
CREATE OR REPLACE FUNCTION pg_catalog.random()
 RETURNS double precision
 LANGUAGE internal
 PARALLEL RESTRICTED STRICT
AS $function$drandom$function$
mydb=> 

19 设置查询结果输出格式(\x [on|off|auto])

mydb=> \x
Expanded display is on.
mydb=> select * from t1 limit 1;
"Here is my list:"
-[ RECORD 1 ]---------------------------
id          | 1
name        | 1_tank
create_time | 2019-07-20 11:03:56.385552

mydb=> 

20 获取元命令的对应代码(psql [-E])

使用psql连接时加上“-E”的选项,在执行元命令后就可以获取对应的SQL代码。

[postgres@tank ~]$ psql -E
psql (10.9)
Type "help" for help.

postgres=# \db
********* QUERY **********
SELECT spcname AS "Name",
  pg_catalog.pg_get_userbyid(spcowner) AS "Owner",
  pg_catalog.pg_tablespace_location(oid) AS "Location"
FROM pg_catalog.pg_tablespace
ORDER BY 1;
**************************

            List of tablespaces
    Name    |  Owner   |      Location      
------------+----------+--------------------
 pg_default | postgres | 
 pg_global  | postgres | 
 tbs_mydb   | pguser   | /pgtbs/10/tbs_mydb
(3 rows)

postgres=# 

参考资料:

《PostgresSQL 10.9》-VI. 参考-PostgreSQL 客户端应用-psql

《PostgreSQL实战》-第2章 客户端工具-2.2 psql功能及应用

https://blog.csdn.net/claroja/article/details/82786379(元命令清单)


Tank

2019.7.20

https://blog.csdn.net/daiyejava

https://www.cnblogs.com/okey