postgresql常用命令集1

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/quhaizhou3/article/details/102764883

文章目录

  1、查看数据库

  2、查看表

  3、查看索引

  4、用通配符模糊查询

  5、显示sql执行时间

  1、查看数据库

       方法一、在操作系统上执行命令

$psql -l
                                     List of databases
     Name     |   Owner   | Encoding |   Collate   |    Ctype    |    Access privileges    
--------------+-----------+----------+-------------+-------------+-------------------------
 db_quhaizhou | quhaizhou | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/quhaizhou          +
              |           |          |             |             | quhaizhou=CTc/quhaizhou
 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)

方法二、登录pg数据库后,查看数据库

   

$ psql
postgres=# \c db_quhaizhou
You are now connected to database "db_quhaizhou" as user "postgres".


db_quhaizhou=# \l
                                     List of databases
     Name     |   Owner   | Encoding |   Collate   |    Ctype    |    Access privileges    
--------------+-----------+----------+-------------+-------------+-------------------------
 db_quhaizhou | quhaizhou | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/quhaizhou          +
              |           |          |             |             | quhaizhou=CTc/quhaizhou
 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)

默认会有一个叫postgres数据,其中db_quhaizhou是创建的数据库


还有两个模板数据库 template0template1.当用户在建数据库时,默认是从模板数据库 template1克隆出来的,所以通常可以定制 template1数据库中的内容,
如往 template0中添加一些表和函数,后续创建的数据库就会继承 template1中的内容,也会拥有这些表和函数。而 template0是一个最简化的模板库,创建数据库时,
如果明确指定从此数据库中继承,将创建出一个最简化的数据库。

  2、查看表

db_quhaizhou-# \d student

包括表结构字段信息及索引信息

  3、查看索引

db_quhaizhou=#\d student_pkey

 4、用通配符模糊查询

    

用通配符查看所有和student有关的表和索引

db_quhaizhou-# \d student*
                 Table "public.student"
 Column |     Type      | Collation | Nullable | Default 
--------+---------------+-----------+----------+---------
 id     | integer       |           | not null | 
 name   | character(32) |           |          | 
 number | character(5)  |           |          | 
Indexes:
    "student_pkey" PRIMARY KEY, btree (id)

  Index "public.student_pkey"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
primary key, btree, for table "public.student"

5、显示sql执行时间

db_quhaizhou-# \timing on 
Timing is on.


db_quhaizhou=# select * from student;
 id |                name                | number 
----+------------------------------------+--------
  1 | 张三                               | 1023 
(1 row)

Time: 0.610 ms

猜你喜欢

转载自blog.csdn.net/quhaizhou3/article/details/102764883