postgresql学习--psql命令行常用命令

\l 列出数据库列表
\d 列出该库下的所有表名称
\d xx 列出xx表的所有列,数据类型,索引等信息
\d x? 通配符,?表示一个字符,即列出所有x字母带头的,只有两个字母的表信息
\d t* 通配符,*表示任意字符,即列出所有t字母带头的表
\c xx 切换到xx数据库
\d+ 显示更多的表信息,比如表大小和描述
\dn 列出所有的schema
\db 列出所有的表空间
\du 列出所有用户的角色
\dp 列出表的权限分配
\encoding utf8  设置客户端的字符集为utf8
\x 行显示模式,也就是mysql的\G
\i xx.sql  运行xx.sql文件
\? 查看更多的\命令




如何显示sql执行时间
mydb=# select * from xx;
 id  | name 
-----+------
 123 | asd
(1 row)


mydb=# \timing on
Timing is on.
mydb=# select * from xx;
 id  | name 
-----+------
 123 | asd
(1 row)


Time: 0.330 ms


如果设置命令行输出格式
\pset border 0 无边框
\pset border 1 边框只在内部
\pset border 2 内外都有边框
mydb=# \pset border 2
Border style is 2.
mydb=# \dp xx
                           Access privileges
+--------+------+-------+-------------------+--------------------------+
| Schema | Name | Type  | Access privileges | Column access privileges |
+--------+------+-------+-------------------+--------------------------+
| public | xx   | table |                   |                          |
+--------+------+-------+-------------------+--------------------------+
(1 row)


mydb=# \pset border 0
Border style is 0.
mydb=# \dp xx
                      Access privileges
Schema Name Type  Access privileges Column access privileges 
------ ---- ----- ----------------- ------------------------
public xx   table                   
(1 row)




实际这些简化的命令行执行的是什么具体的SQL
运行psql时添加-E参数
[postgresql@10-4-33-189 bin]$ ./psql -h127.0.0.1 -p 5432 -E  mydb
psql (9.4.1)
Type "help" for help.


mydb=# \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' 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','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 | test | table | postgresql
 public | xd   | table | postgresql
 public | xx   | table | postgresql
(3 rows)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值