Welcome 微信登录

首页 / 数据库 / MySQL / PostgreSQL数据库对象管理之非模式对象

数据库对象管理之非模式对象数据库对象:表,索引,视图,图表,缺省值,规则,触发器,语法等。所有在数据库中的一切,都可以被称为数据库对象。按照一般划分,数据库对象分为模式对象和非模式对象模式对象:特定数据库对象的集合。如:表、索引等。非模式对象:其他数据库对象。如用户、权限、表空间等一非模式对象及相关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 test2select * from test1.t1; --报错,无权限c test test1grant select on test1.t1 to test2;  --将t1表的select权限赋予test2--grant select on all tables in schema test1 to test2;-- --授予批量查表权限c test test2select * from test1.t1; --报错,无权限c test test1Grant usage on schema test1 to test2; --将shema test1的usage权赋给test2c test test2select * from test1.t1; --正确结果提示:如果test1是建立在public模式下,则将test1赋权之后,可以直接访问。 --示例权限级联授予与回收c test dba --切换到dba用户create user test3 with password  "123456";c test test2Grant usage on schema test1 to test3; --警告:  没有为"test1"授予权限c test test1revoke 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 test2grant select on test1.t1 to test3;grant usage on schema test1 to test3; --赋权成功c test  test3select * from test1.t1;c test  test1revoke usage on schema test1 from test2;--要求CASCADE会级联收回revoke select on test1.t1 from test2;ERROR:  dependent privileges existHINT:  Use CASCADE to revoke them too.STATEMENT:  revoke select on test1.t1 from test2;ERROR:  dependent privileges existHINT:  Use CASCADE to revoke them too.revoke usage on schema test1 from test2 CASCADE;revoke select on test1.t1 from test2 CASCADE;--示例权限组c  test dbatest=# 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 test2select *  from test1.t1;
2.4例某些生产环境不允许任何用户在public模式下创建对象(即某些用户只能查询的权限)test=# create user test4 with password "123456";test=# revoke  create on schema public from public;REVOKEtest=# c  test test4test=> create table tbtest4(id int);错误:  对模式 public 权限不够test=> c  test dbatest=# grant create  on schema public to test4;test=# c  test test4test=> 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<!-- Baidu Button BEGIN --> ------------------------------------华丽丽的分割线------------------------------------在CentOS 6.5上编译安装PostgreSQL 9.3数据库 http://www.linuxidc.com/Linux/2016-06/132272.htmCentOS 6.3环境下yum安装PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htmPostgreSQL缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htmWindows平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htmUbuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htmUbuntu上的phppgAdmin安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htmCentOS平台下安装PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htmPostgreSQL配置Streaming Replication集群 http://www.linuxidc.com/Linux/2014-05/101724.htm------------------------------------华丽丽的分割线------------------------------------PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里本文永久更新链接地址