postgresql 查看数据库,表,索引,表空间以及大小

本文详细介绍了如何使用 PostgreSQL 进行数据库、表、索引、表空间及大小的查看与管理,包括多表与单表的查看、索引管理、表空间大小查看等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

postgresql 查看数据库,表,索引,表空间以及大小

1.查看数据库
#psql -U postgres

//\加上字母l,相当于mysql的,mysql> show databases;
postgres-# \l

//查看testuser数据库的大小
postgres-# select pg_database_size('postgres');

//查看所有数据库的大小
select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database;

//以KB,MB,GB的方式来查看数据库大小
select pg_size_pretty(pg_database_size('postgres'));

2.查看多表
psql -U testuser -d testdb
//相当于mysql的,mysql> show tables;
testuser=> \dt                       
        List of relations 
 Schema | Name | Type  |  Owner 
--------+------+-------+--------- 
 public | test | table | testuser 

3.查看单表
//相当于mysql的,mysql> desc test;
testuser=> \d test;                  
            Table "public.test" 
 Column |         Type          | Modifiers 
--------+-----------------------+----------- 
 id     | integer               | not null 
 name   | character varying(32) | 
Indexes: "testuser_id_pk" PRIMARY KEY, btree (id) 

//查看表大小
testuser=> select pg_relation_size('test');
 pg_relation_size 
------------------ 
                0 
(1 row) 

//以KB,MB,GB的方式来查看表大小 
testuser=> select pg_size_pretty(pg_relation_size('test'));
 pg_size_pretty 
---------------- 
 0 bytes 
(1 row) 

//查看表的总大小,包括索引大小
testuser=> select pg_size_pretty(pg_total_relation_size('test'));
 pg_size_pretty 
---------------- 
 8192 bytes 
(1 row) 


4.查看索引
//相当于mysql的,mysql> show index from test;
testuser=> \di 
                List of relations 
 Schema |     Name      | Type  |  Owner  | Table 
--------+---------------+-------+---------+------- 
 public | testuser_id_pk | index | playboy | test 
(1 row) 
  //查看索大小
testuser=> select pg_size_pretty(pg_relation_size('playboy_id_pk'));     
 pg_size_pretty 
 ---------------- 
  8192 bytes 
 (1 row) 

 
5.查看表空间,以及大小
//查看所有表空间
testuser=> select spcname from pg_tablespace;          
  spcname 
------------ 
 pg_default 
 pg_global 
(2 rows) 

//查看表空间大小 
testuser=> select pg_size_pretty(pg_tablespace_size('pg_default'));    
 pg_size_pretty 
 ---------------- 
  14 MB 
 (1 row) 

转载于:https://my.oschina.net/jimyao/blog/612669

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值