您的位置:首页 > 数据库

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

2016-03-30 15:30 603 查看
\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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: