数据库对象管理之非模式对象
数据库对象:表,索引,视图,图表,缺省值,规则,触发器,语法等。所有在数据库中的一切,都可以被称为数据库对象。
按照一般划分,数据库对象分为模式对象和非模式对象
模式对象:特定数据库对象的集合。如:表、索引等。
非模式对象:其他数据库对象。如用户、权限、表空间等
一非模式对象及相关
1用户与角色
(1)用户(user):用户用来访问和管理数据库,具有一系列的权限,如登陆、建表、建索引等。
(2)角色(role):和用户相同,唯一的区别是默认无法登陆,通常作为权限角色组。
|
create role testrole1;//角色,不可登陆 create user testuser1; //用户,可登陆 select * from pg_roles; select * from pg_user; select * from pg_authid;//表中rolcanlogin决定是否可以登录 修改用户testuser1不可登陆 update pg_authid set rolcanlogin='f' where rolname='testuser1'; 修改用户testuser1可登陆 ALTER ROLE testuser1 WITH LOGIN; |
(3)角色组:便于权限授予控制,可以把各种权限统一授予给某个角色组,再把角色组授予给特定用户。
通过\h create user和\h create role来对比
|
查看数据库用户: \du 或者 select * from pg_user; select * from pg_shadow; // 需要dba权限,显示密码 创建用户:\h create user (也可通过外部命令创建) create user test001 with password '123456';// 密码加密 create user test002 with unencrypted password '123456';// 密码不加密 修改用户:\h alter user Alter user test001 with password '654321';//修改密码 删除用户:drop user test001; --删除用户之前需要删除所属他的表和schema |
2权限管理
少部分权限:不会级联回收。只能由superuser去回收。
部分权限:用户对其他用户的数据对象操作的权限。会级联回收。
级联授权:A用户授权B用户可以使用with grant option参数指定级联权限。
级联回收:A用户给了B用户某个权限,而B用户又将该权限赋予C用户。当A收回给B用户权限的时候,会要求cascade级联回收。
2.1创建用户的时候授权
(修改方式alterrole 名称 with 权限)
|
superuser/nosuperuser 超级用户权限 createdb/nocreatedb 创建数据库的权限 createrole/nocreaterole 创建角色的权限 createuser/nocreateuser 创建用户的权限 inherit/noinherit 可继承的权限 login/nologin 可登陆权限 replication/noreplication 流复制专用用户 connection limit 连接限制(默认-1,无限制) |
2.2 grant赋权(revoke 收回)
语法:grant 权限 on 数据库对象的类型(database,table,sequence,schema) to 用户
|
ALL 所有权限 CREATE 对于数据库时创建模式,对于模式是创建对象,对于表空间是创建对象在表空间 DELETE 删除权限 INSERT 插入权限 SELECT 查找权限 TEMPORARY 创建临时表 TRUNCATE 清空表 USAGE 使用权限 CONNECT 连接权限 EXECUTE 执行权限 REFERENCES 创建外键约束 TRIGGER 指定表示创建触发器 UPDATE 更新 test=# \h GRANT (\h REVOKE) GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER } [, ...] | ALL [ PRIVILEGES ] } ON { [ TABLE ] 表名 [, ...] | ALL TABLES IN SCHEMA 模式名称 [, ...] } TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( 列名称 [, ...] ) [, ...] | ALL [ PRIVILEGES ] ( 列名称 [, ...] ) } ON [ TABLE ] 表名 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { { USAGE | SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON { SEQUENCE 序列名称 [, ...] | ALL SEQUENCES IN SCHEMA 模式名称 [, ...] } TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] } ON DATABASE 数据库名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { USAGE | ALL [ PRIVILEGES ] } ON DOMAIN 域_名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { USAGE | ALL [ PRIVILEGES ] } ON FOREIGN DATA WRAPPER 外部数据封装器的名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { USAGE | ALL [ PRIVILEGES ] } ON FOREIGN SERVER 服务器名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { EXECUTE | ALL [ PRIVILEGES ] } ON { FUNCTION 函数名称 ( [ [ 参数模式 ] [ 参数名称 ] 参数类型 [, ...] ] ) [, ...]| ALL FUNCTIONS IN SCHEMA 模式名称 [, ...] } TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { USAGE | ALL [ PRIVILEGES ] } ON LANGUAGE 语言名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON LARGE OBJECT loid [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] } ON SCHEMA 模式名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { CREATE | ALL [ PRIVILEGES ] } ON TABLESPACE 表空间的名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT { USAGE | ALL [ PRIVILEGES ] } ON TYPE 类型名称 [, ...] TO { [ GROUP ] 角色名称 | PUBLIC } [, ...] [ WITH GRANT OPTION ] GRANT 角色名称 [, ...] TO 角色名称 [, ...] [ WITH ADMIN OPTION ] |
2.3例对象权限授予与收回
|
--示例权限授予与收回 test=# create user test1 with password '123456'; test=# create user test2 with password '123456'; test=#CREATE SCHEMA test1 AUTHORIZATION test1; test=#CREATE SCHEMA test2 AUTHORIZATION test2; \c test test1 --切换到test1用户 create table t1 (id int); insert into t1 values(1); \c test test2 select * from test1.t1; --报错,无权限 \c test test1 grant select on test1.t1 to test2; --将t1表的select权限赋予test2 --grant select on all tables in schema test1 to test2;-- --授予批量查表权限 \c test test2 select * from test1.t1; --报错,无权限 \c test test1 Grant usage on schema test1 to test2; --将shema test1的usage权赋给test2 \c test test2 select * from test1.t1; --正确结果 提示:如果test1是建立在public模式下,则将test1赋权之后,可以直接访问。 --示例权限级联授予与回收 \c test dba --切换到dba用户 create user test3 with password '123456'; \c test test2 Grant usage on schema test1 to test3; --警告: 没有为"test1"授予权限 \c test test1 revoke usage on schema test1 from test2; --回收test2对于schema test1的权限 grant usage on schema test1 to test2 with grant option; --使用grant选项赋权 revoke select on test1.t1 from test2; grant select on test1.t1 to test2 with grant option; \c test test2 grant select on test1.t1 to test3; grant usage on schema test1 to test3; --赋权成功 \c test test3 select * from test1.t1; \c test test1 revoke usage on schema test1 from test2;--要求CASCADE会级联收回 revoke select on test1.t1 from test2; ERROR: dependent privileges exist HINT: Use CASCADE to revoke them too. STATEMENT: revoke select on test1.t1 from test2; ERROR: dependent privileges exist HINT: Use CASCADE to revoke them too. revoke usage on schema test1 from test2 CASCADE; revoke select on test1.t1 from test2 CASCADE; --示例权限组 \c test dba test=# grant select on test1.t1 to testx; test=# grant usage on schema test1 to testx; test=# grant testx to test2; -- ALTER GROUP testx ADD USER test3; (ALTER GROUP testx DROP USER test3;) \c test test2 select * from test1.t1; |
2.4例
某些生产环境不允许任何用户在public模式下创建对象(即某些用户只能查询的权限)
test=# create user test4 with password '123456';
test=# revoke create on schema public from public;
REVOKE
test=# \c test test4
test=> create table tbtest4(id int);
错误: 对模式 public 权限不够
test=> \c test dba
test=# grant create on schema public to test4;
test=# \c test test4
test=> create table tbtest4(id int);
//给test4用户授权查public模式下的所有表
upbase=# grant select on all tables in schema public to test4;
//给test4用户授权查public模式下以后新建的所有表
upbase=# alter default privileges in schema public grant select on tables to test4;
如果是其他模式同理
grant select on alltables in schema XX to username;
alter default privileges in schema XX grant select on tables to username;
3空间管理
定义空间只是定义一个目录,抽象概念。
3.1创建表空间
操作系统创建目录:mkdir -p /test/data/tablespace_test
数据库sql命令:
不指定表空间所属用户,表空间属于创建该表空间的用户。
create tablespace tablespace_test location '/test/data/tablespace_test';
指定表空间所属用户
create tablespace tablespace_test owner test1location
'/test/data/tablespace_test';
3.2其他相关命令
(1)查看表空间:
\db or select *from pg_tablespace;
(2)查看表空间大小:
select pg_size_pretty(pg_tablespace_size('pg_default'));
(3)设置默认的表空间:
SET default_tablespace = tablespace_test;
(4)将表创建到指定的表空间:
CREATE TABLE test(id int) TABLESPACE tablespace_test;
ok
------------------------------------华丽丽的分割线------------------------------------
在CentOS 6.5上编译安装PostgreSQL 9.3数据库 http://www.linuxidc.com/Linux/2016-06/132272.htm
CentOS 6.3环境下yum安装PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htm
PostgreSQL缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htm
Windows平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htm
Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htm
Ubuntu上的phppgAdmin安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htm
CentOS平台下安装PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htm
PostgreSQL配置Streaming Replication集群 http://www.linuxidc.com/Linux/2014-05/101724.htm
------------------------------------华丽丽的分割线------------------------------------
PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137239.htm
