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)
\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)
相关文章推荐
- PostgreSQL 回归测试
- Gpload error: [could not execute SQL in sql:after "Vacuum analyze
- 关于SQL Server链接的问题
- 初用oracle 笔记
- MySQL备份脚本的一个简单小脚本
- Memcache知识点梳理
- windows下mysql自动定时备份bat
- oracle数据库各版本下载地址
- SQL中Group By的使用
- redis配置文件redis.conf详细说明
- mysql的类dblink方案
- 分页sql脚本
- SQL SERVER 合并重复行,行列转换
- redis启动
- MYSQL COALESCE 用法
- Oracle性能优化--DBMS_PROFILER
- MySQL数据库SYS CPU高的可能性分析
- MySQL备份和恢复实战
- Oracle性能优化--AUTOTRACE 操作
- oracle11.2.0.1新特性