翻译:
作者: | SathiyaMoorthy |
---|---|
原文: | http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-commands-with-examples/ |
piglei(piglei2007[AT]gmail.com) |
现在有不少开源软件都在使用postgresql作为它们的数据库系统。但公司可能不会招一些全职的postgresql DBA来维护它(piglei: 在国内基本也找不到)。而会让一些比如说Oracle DBA、Linux系统管理员或者程序员去 维护。在这篇文章中我们会介绍15个无论是对psql老鸟还是DBA都非常实用的postgresql数据库命令。
在这里可以找到我们的前一篇文章:15 Practical PostgreSQL DBA Commands
1. 如何找到postgresql数据库中占空间最大的表?
- $ /usr/local/pgsql/bin/psql test
- Welcome to psql 8.3.7,the Postgresql interactive terminal.
- Type: \copyright for distribution terms
- \h for help with sql commands
- \? for help with psql commands
- \g or terminate with semicolon to execute query
- \q to quit
- test=# SELECT relname,relpages FROM pg_class ORDER BY relpages DESC;
- relname | relpages
- -----------------------------------+----------
- pg_proc | 50
- pg_proc_proname_args_nsp_index | 40
- pg_depend | 37
- pg_attribute | 30
如果你只想要最大的那个表,可以用limit参数来限制结果的数量,就像这样:
- # SELECT relname,relpages FROM pg_class ORDER BY relpages DESC limit 1;
- relname | relpages
- ---------+----------
- pg_proc | 50
- (1 row)
- relname- 关系名/表名
- relpages- 关系页数(默认情况下一个页大小是8kb)
- pg_class- 系统表,维护着所有relations的详细信息
- limit 1- 限制返回结果只显示一行
2. 如何计算postgresql数据库所占用的硬盘大小?
pg_database_size这个方法是专门用来查询数据库大小的,它返回的结果单位是字节(bytes)。:
- # SELECT pg_database_size('geekdb');
- pg_database_size
- ------------------
- 63287944
- (1 row)
如果你想要让结果更直观一点,那就使用**pg_size_pretty**方法,它可以把字节数转换成更友好易读的格式。
- # SELECT pg_size_pretty(pg_database_size('geekdb'));
- pg_size_pretty
- ----------------
- 60 MB
- (1 row)
3. 如何计算postgresql表所占用的硬盘大小?
下面这个命令查出来的表大小是包含索引和toasted data的,如果你对除去索引外仅仅是表占的大小感兴趣,可以 使用后面提供的那个命令。
- # SELECT pg_size_pretty(pg_total_relation_size('big_table'));
- pg_size_pretty
- ----------------
- 55 MB
- (1 row)
使用**pg_relation_size**而不是**pg_total_relation_size**方法。
- # SELECT pg_size_pretty(pg_relation_size('big_table'));
- pg_size_pretty
- ----------------
- 38 MB
- (1 row)
4. 如何查看postgresql表的索引?
- Syntax: # \d table_name
让我们看下面这个例子,注意如果你的表有索引的话,你会在命令输出内容的后面那部分找到一个标题Indexes,在这个例子中,pg_attribut表有两个btree类型的索引,默认情况下postgresql使用的索引类型都 是btree,因为它适用于绝大多数情况。
- test=# \d pg_attribute
- Table "pg_catalog.pg_attribute"
- Column | Type | Modifiers
- ---------------+----------+-----------
- attrelid | oid | not null
- attname | name | not null
- atttypid | oid | not null
- attstattarget | integer | not null
- attlen | smallint | not null
- attnum | smallint | not null
- attndims | integer | not null
- attcacheoff | integer | not null
- atttypmod | integer | not null
- attbyval | boolean | not null
- attstorage | "char" | not null
- attalign | "char" | not null
- attnotnull | boolean | not null
- atthasdef | boolean | not null
- attisdropped | boolean | not null
- attislocal | boolean | not null
- attinhcount | integer | not null
- Indexes:
- "pg_attribute_relid_attnam_index" UNIQUE,btree (attrelid,attname)
- "pg_attribute_relid_attnum_index" UNIQUE,attnum)
5. 如何创建一个指定类型的索引?
默认情况下的索引都是btree类型的,但是你可以用下面的方法来指定新索引的类型。
- Syntax: CREATE INDEX name ON table USING index_type (column);
- # CREATE INDEX test_index ON numbers using hash (num);
6. 如何在postgresql中使用事务?
如何开始一个事务?
- # BEGIN -- 开始事务
如何提交或回滚一个事务?
只有当你调用COMMIT命令后,你在BEGIN命令后所做的所有操作才会真正的被提交到postgresql数据库。另外你还 可以使用ROLLBACK命令来回滚事务中做的所有操作。
- # ROLLBACK -- 回滚当前事务
- # COMMIT -- 提交当前事务
9. 怎么生成一个序列的数字并把它们插入到一个表中?
下面这个命令将会生成1到1000这一千个数字并插入到numbers表中。
- # INSERT INTO numbers (num) VALUES ( generate_series(1,1000));
10. 如何统计postgresql表里面的行数?
这个命令可以查询出表里所有记录的条数。
- # select count(*) from table;
这个命令会查询出表中指定列的值不为空的所有行数.
- # select count(col_name) from table;
这个命令会查询出表中按制定列的值去重后的总行数。
- # select count(distinct col_name) from table;
11. 如何查询表中某列**第二大**的值?
查询某列最大的值
- # select max(col_name) from table;
查询某列中第二大的值
- # SELECT MAX(num) from number_table where num < ( select MAX(num) from number_table );
12. 如何查询表中某列**第二小**的值?
查询某列最小的值
- # select min(col_name) from table;
查询某列第二小的值
- # SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table );
13. 如何列出postgresql数据库中基本的数据类型?
下面截取了部分内容,这个命令可以展示可用的数据类型和它们所占用的字节数。
- test=# SELECT typname,typlen from pg_type where typtype='b';
- typname | typlen
- ----------------+--------
- bool | 1
- bytea | -1
- char | 1
- name | 64
- int8 | 8
- int2 | 2
- int2vector | -1
- typname - 类型的名称
- typlen - 类型的大小
14. 如何把某一次查询的结果保存为一个文件?
- # \o output_file
- # SELECT * FROM pg_class;
上面这个查询的结果将会被保存到到"output_file"文件中。当重定向被激活后,之后的所有查询都不再会把结果 打印在屏幕上了。如果要再次打开屏幕输出,需要再执行一次不带任何参数的 o 命令。
- # \o
我们之前的文章还有提到过,你可以使用pg_dump和psql来备份和恢复你的数据库
15. 存储加密后的密码
Postgresql数据库可以使用下面的crypt命令来加密数据。这可以用来方便的用来保存你的用户名和密码。
- # SELECT crypt ( 'sathiya',gen_salt('md5') );
crypt在你的环境下可能会用不了,并提供下面这个报错信息。
- ERROR: function gen_salt("unknown") does not exist
- HINT: No function matches the given name and argument types.
- You may need to add explicit type casts.
解决方法:
为了解决这个问题,你需要安装 postgresql-contrib-版本 这个包,然后在psql中执行下面这个命令。
原文后的评论
在第13个命令中,那个typtype='b'是什么意思?
typtype='b'表示basetype。b==basetype.
Postgresql有这么几种数据类型: composite types,domains,and pseudo-types.
http://developer.postgresql.org/pgdocs/postgres/extend-type-system.html
COUNT(*)效率问题
在大表上执行count(*)会有比较明显的效率问题
原文地址:http://www.zlovezl.cn/articles/15-advanced-postgresql-commands-with-examples/