一、Postgresql 安装
tar zxvf postgresql-9.4.4.tar.gz
cd postgresql-9.4.4
./configure --prefix=/usr/local/postgres --with-pgport=1921 --with-perl \
--with-python --with-openssl --with-pam --without-ldap --with-libxml \
--with-libxslt --with-wal-blocksize=16 --with-blocksize=16 \
--enable-dtrace --enable-debug
最简单的编译:
/configure --prefix=/usr/local/postgres --with-perl --with-python
安装依赖包:
yum install systemtap systemtap-sdt-devel -y
yum -y install readline-devel readline -y
yum install zlib-devel -y
yum install openssl-devel -y
yum install pam-devel -y
yum install libxml2-devel -y
yum install libxslt-devel -y
yum install python-devel -y
其中:
安装systemtap时,安装了systemtap-devel、systemtap-client和systemtap-runtime这3个包。
systemtap-sdt-devel是编译PostgreSQL时,选项--enable-dtrace需要的包。
gmake world
gmake check-world (需要使用普通用户执行,可选,可以不执行)
gmake install-world
初始化数据库:
initdb -D $PGDATA --locale=en_US.UTF8 --pwprompt -A password
安装contrib目录下的工具
cd postgres/contrib
make
make install
postgresql启动与关闭
su - postgres
pg_ctl start -D $PGDATA
pg_ctl stop -m fast -D $PGDATA
pg_ctl reload
关闭的几种模式:
fast:相当于oracle的immediate
immediate:相当于oracle的abort
smart(默认):相当于oracle的正常关闭
二、安装后工作
1.添加新用户和新数据库
初次安装后,默认生成一个名为postgres的数据库和一个名为postgres的数据库用户。这里需要注意的是,同时还生成了一个名为postgres的Linux系统用户
方法1:
su - postgres
psql postgres
为用户postgres设置一个密码:
\password postgres
创建用户
create user dbuser with password '123456';
创建数据库
create database exampledb owner dbuser;
将expampledb数据库的所有权限都赋予dbuser,否则dbuser只能登录控制台
grant all privileges on database exampledb to dbuser;
查看用户
\du
退
\q
列出当前数据库和连接信息
\conninfo
方法2:
安装完后,要添加用户和新数据库
su - postgres createuser --supperuser dbuser
su - postgres posql
\password dbuser
\q
su - u postgres createdb -O dbuser exampledb
2.连接数据库
psql -U dbuser -d exampledb -h 127.0.0.1 -p 1921
错误:
psql: FATAL: no pg_hba.conf entry for host "127.0.0.1", user "dbuser", database "exampledb", SSL off
在其下加上请求连接的机器IP
vi pg_hba.conf
host all all 127.0.0.1/32 md5
控制台命令
\h
\h select
\?
\l:列出所有数据库
\c [database name]:连接数据库
\d [table name]:列出表结构
\du:列出所有用户
\conninfo:列出当前数据库和连接信息
3.给对象其它用户访问权限
默认情况下,只有数据库的所有者才有对拥有表进行操作,要允许其它用户使用这个数据库,必须赋予相就的权限
创建另一个账号:
create user ch with password '123456';
create database ywhjdb owner ch;
grant all privileges on database ywhjdb to ch;
连接到exmapledb库
psql -U dbuser -d exampledb -h 127.0.0.1 -p 1921
create table t1 (id int);
\d
\du+
赋予ch用户对象只读权限
grant select on t1 to ch;
赋予ch用户对象所有权限
grant all on t1 to ch;
回收权限:
revoke select on t1 from ch;
4.连接其它数据库
\c ywhjdb ch
5.修改数据库当前owner
以管理员账号执行
alter database ywhjdb owner to dbuser;
6.关于账号权限案例
create database test;
create user sh1 password '123456';
create user sh2 password '123456';
alter database test owner to sh1;
grant select,update,insert on t1 to sh2;
drop user sh2;
7.创建库表案例
创建用户
create user webuser with password '123456';
创建表空间
mkdir -p /usr/local/postgres/data/webdata
CREATE TABLESPACE tbs_webdata OWNER webuser LOCATION '/usr/local/postgres/data/webdata';
建库
CREATE DATABASE webdb
WITH ENCODING='UTF8'
OWNER=webuser
TEMPLATE=template0
LC_COLLATE='en_US.UTF8'
LC_CTYPE='en_US.UTF8'
CONNECTION LIMIT=-1
TABLESPACE=tbs_webdata;
连接webdb库
psql -U
webuser -d webdb -h 127.0.0.1 -p 1921
创建SCHEMA:
CREATE SCHEMA webuser;
建表
CREATE TABLE webuser.LOGUSER (
ID bigint NOT NULL,
USERNAME varchar(100) NOT NULL,
DISPLAYNAME varchar(100) NOT NULL,
PASSWORD varchar(50) NOT NULL,
CONSTRAINT PK_USER PRIMARY KEY (ID),
CONSTRAINT UK_USER UNIQUE (USERNAME)
);
8.执行计划
explain verbose:显示详细的附加信息
explain analyze:会真正执行SQL,对于DML操作,会有问题,可以放到一事务中
begin
explain analyze sql
rollback;
explain (format json) select * from abc;
explain select * from abc;
explain analyze select * from abc;
(format json) select * from abc;
QUERY PLAN
--------------------------------
[ +
{ +
"Plan": { +
"Node Type": "Seq Scan",+
"Relation Name": "abc", +
"Alias": "abc", +
"Startup Cost": 0.00, +
"Total Cost": 58.10, +
"Plan Rows": 4810, +
"Plan Width": 4 +
} +
} +
]
访问类型:
全表扫描:Seq Scan
hash,hash join
create table test01 (id int primary key,note text) ;
insert into test01 select generate_series(1,100000),'testcontent';
explain select id from test01 where id>10 and id<20;
Index Only Scan using test01_pkey on test01
explain analyze select id from test01 where id>10 and id<20;
Index Only Scan using test01_pkey on test01 (cost=0.29..8.49 rows=10 width=4) (actual time=0.023..0.024 rows=9 loops=1)
Index Cond: ((id > 10) AND (id < 20))
Heap Fetches: 9
Planning time: 0.103 ms
Execution time: 0.049 ms
(5 rows)
说明:Heap Fetches: 9,从数据块中读取9行,这实际是上数据块所有的行,说明index only scan没有起使用,没走索引,要痕量一个表的index only scan的效果,关键在于
这个表的数据块有多少是对所有事务可见的。
查这个表的数据块有多少是对所有事务可见的:
select relname,relpages,relallvisible from pg_class where relname='test01';
relname | relpages | relallvisible
---------+----------+---------------
test01 | 270 | 0
relallvisible =0 说明index only scan没有任何效果,这时对这个表执行vacuum
vacuum test01;
select relname,relpages,relallvisible from pg_class where relname='test01';
relname | relpages | relallvisible
---------+----------+---------------
test01 | 270 | 270
explain analyze select id from test01 where id>10 and id<20;
Index Only Scan using test01_pkey on test01 (cost=0.29..4.49 rows=10 width=4) (actual time=0.016..0.018 rows=9 loops=1)
Index Cond: ((id > 10) AND (id < 20))
Heap Fetches: 0
Planning time: 0.128 ms
Execution time: 0.043 ms
Heap Fetches: 0:指从表的数据文件中读取的行数为0,说明所有的数据都是从索引中读取到的。
查看索引、表列:
\d test01;
手工收集统计
analyze test01; //默认分析所有字段
analyze test01(id); //分析特定字段
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/91975/viewspace-1749933/
,如需转载,请注明出处,否则将追究法律责任。