elations
Schema | Name | Type | Owner | Table
--------+-----------+-------+----------+-------
public | idx_cndba | index | postgres | cndba
(1 row)
?
--查看索引大小:
postgres=# select pg_size_pretty(pg_relation_size('idx_cndba'));
pg_size_pretty
----------------
16kB
(1 row)
?
--看表的总大小,包括索引大小
postgres=# selectpg_size_pretty(pg_total_relation_size('cndba'));
pg_size_pretty
----------------
24kB
(1 row)
?
--查看所有表空间:
postgres=# select spcname frompg_tablespace;
spcname
------------
pg_default
pg_global
(2 rows)
?
--查看表空间大小:
postgres=# selectpg_size_pretty(pg_tablespace_size('pg_default'));
pg_size_pretty
----------------
23MB
(1 row)
?
?
5 PostgreSQL用户认证
?
PostgreSQL的配置文件在/etc/postgresql目录下:
dave@dave:/etc/postgresql/9.1/main$ pwd
/etc/postgresql/9.1/main
dave@dave:/etc/postgresql/9.1/main$ ls
environment pg_ctl.conf pg_hba.conf pg_ident.conf postgresql.conf start.conf
?
postgresql.conf 文件里保存的是数据库的相关的配置。
?
# - Connection Settings -
?
listen_addresses = '*' # what IP address(es) to listen on;
#comma-separated list of addresses;
#defaults to 'localhost', '*' = all
#(change requires restart)
port = 5432 # (change requiresrestart)
max_connections = 100 # (change requires restart)
# Note: Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (seemax_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directory ='/var/run/postgresql' # (changerequires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
#(change requires restart)
#bonjour = off # advertise servervia Bonjour
#(change requires restart)
#bonjour_name = '' # defaults to thecomputer name
#(change requires restart)
?
注意这里的端口信息,要添加的防火墙的策略里。
?
?
pg_hba.conf中保存基于主机的认证规则。每条规则会被逐条应用,直到找到一条符合的,就能通过认证;或者访问被reject方法显式拒绝。
?
postgres@dave:/etc/postgresql/9.1/main$ catpg_hba.conf |grep -v ^# |grep -v ^$
local all postgres trust
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
?
?
带注释的如下:
# Database administrative login by Unixdomain socket
local all postgres trust
?
# TYPE DATABASE USER ADDRESS METHOD
?
# "local" is for Unix domainsocket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
?
?
Type = host表示远程连接。
Database = all 表示所有数据库。
User = all 表示所有用户。
ADDRESS 由两部分组成,即IP地址/子网掩码。子网掩码规定了IP地址中前面哪些位表示网络编号。这里/0表示IP地址中没有表示网络编号的位,这样的话全部的IP地址都匹配,例如192.168.0.0/24表示匹配前24位,所以它匹配任何192.168.0.x形式的IP地址。
Method = trust 实际上表示无需认证。
?
?
--允许在本机上的任何身份连接任何数据库
TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
local all all trust(无条件进行连接)
?
--允许IP地址为192.168.1.x的任何主机与数据库sales连接
TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host sales all 192.168.1.0 255.255.255.0 identsameuser
?
?
6 远程访问数据库
postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下:
(1)postgresql.conf
将该文件中的listen_addresses项值设定为*。
?
(2)pg_hba.conf
在该配置文件的host allall 127.0.0.1/32 md5行下添加以下配置,或者直接将这一行修改为以下配置
host all all 0.0.0.0/0 md5
?
表示允许所有IP访问。
?
修改之后,Reload 或者重启数据库让修改生效:
postgres@dave:/etc/postgresql/9.1/main$/etc/init.d/postgresql reload
?
?
?
直接使用Navicat 链接:

?
