Postgresql的用户管理二

五、给已存在用户赋予各种权限

使用ALTER ROLE 命令。

ALTER ROLE 语法:


ALTER ROLE name [ [ WITH ] option [ ... ] ]

where option can be:

SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| CREATEUSER | NOCREATEUSER
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| CONNECTION LIMIT connlimit
| [ ENCRYPTED | UNENCRYPTED ] PASSWORD ‘password‘
| VALID UNTIL ‘timestamp‘

ALTER ROLE name RENAME TO new_name

ALTER ROLE name [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER ROLE name [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
ALTER ROLE name [ IN DATABASE database_name ] RESET configuration_parameter
ALTER ROLE name [ IN DATABASE database_name ] RESET ALL

5.1
赋予bella 登录权限

a.
查看现在的角色属性


postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB, Cannot login | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create DB | {}
sandy | | {}

postgres=#

b.
赋予登录权限


postgres=# ALTER ROLE bella WITH LOGIN;
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create DB | {}
sandy | | {}

postgres=#

5.2
赋予renee 创建角色的权限


postgres=# ALTER ROLE renee WITH CREATEROLE;
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create role, Create DB | {}
sandy | | {}

postgres=#

5.3
赋予david 带密码登录权限

postgres=# ALTER ROLE david WITH PASSWORD ‘ufo456‘;
ALTER ROLE
postgres=#

5.4
设置sandy 角色的有效期


postgres=# ALTER ROLE sandy VALID UNTIL ‘2014-04-24‘;
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create role, Create DB | {}
sandy | | {}

postgres=# SELECT * from pg_roles ;
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | oid
----------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+------------------------+-----------+-------
postgres | t | t | t | t | t | t | t | -1 | ******** | | | 10
bella | f | t | f | t | f | t | f | -1 | ******** | | | 49440
renee | f | t | t | t | f | t | f | -1 | ******** | | | 49442
david | f | t | f | f | f | t | f | -1 | ******** | | | 49438
sandy | f | t | f | f | f | t | f | -1 | ******** | 2014-04-24 00:00:00+08 | | 49439
(5 rows)

postgres=#

六、角色赋权/角色成员

在系统的角色管理中,通常会把多个角色赋予一个组,这样在设置权限时只需给该组设置即可,撤销权限时也是从该组撤销。在PostgreSQL中,首先需要创建一个代表组的角色,之后再将该角色的membership
权限赋给独立的角色即可。

6.1
创建组角色


postgres=# CREATE ROLE father login nosuperuser nocreatedb nocreaterole noinherit encrypted password ‘abc123‘;
CREATE ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB | {}
david | | {}
father | No inheritance | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create role, Create DB | {}
sandy | | {}

postgres=#

6.2 给father
角色赋予数据库test 连接权限和相关表的查询权限。


postgres=# GRANT CONNECT ON DATABASE test to father;
GRANT
postgres=# \c test renee
You are now connected to database "test" as user "renee".
test=> \dt
No relations found.
test=> CREATE TABLE emp (
test(> id serial,
test(> name text);
NOTICE: CREATE TABLE will create implicit sequence "emp_id_seq" for serial column "emp.id"
CREATE TABLE
test=> INSERT INTO emp (name) VALUES (‘david‘);
INSERT 0 1
test=> INSERT INTO emp (name) VALUES (‘sandy‘);
INSERT 0 1
test=> SELECT * from emp;
id | name
----+-------
| david
| sandy
(2 rows)

test=> \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | emp | table | renee
(1 row)

test=> GRANT USAGE ON SCHEMA public to father;
WARNING: no privileges were granted for "public"
GRANT
test=> GRANT SELECT on public.emp to father;
GRANT
test=>

6.3
创建成员角色


test=> \c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# CREATE ROLE son1 login nosuperuser nocreatedb nocreaterole inherit encrypted password ‘abc123‘;
CREATE ROLE
postgres=#

这里创建了son1
角色,并开启inherit 属性。PostgreSQL
里的角色赋权是通过角色继承(INHERIT)的方式实现的。

6.4 将father
角色赋给son1

postgres=# GRANT father to son1;
GRANT ROLE
postgres=#

还有另一种方法,就是在创建用户的时候赋予角色权限。

postgres=# CREATE ROLE son2 login nosuperuser nocreatedb nocreaterole inherit encrypted password ‘abc123‘ in role father;
CREATE ROLE
postgres=#

6.5
测试son1 角色


postgres=# \c test son1
You are now connected to database "test" as user "son1".
test=> \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | emp | table | renee
(1 row)

test=> SELECT * from emp;
id | name
----+-------
| david
| sandy
(2 rows)

test=>

用renee
角色新创建一张表,再次测试


test=> \c test renee
You are now connected to database "test" as user "renee".
test=> CREATE TABLE dept (
test(> deptid integer,
test(> deptname text);
CREATE TABLE
test=> INSERT INTO dept (deptid, deptname) values(1, ‘ts‘);
INSERT 0 1
test=> \c test son1
You are now connected to database "test" as user "son1".
test=> SELECT * from dept ;
ERROR: permission denied for relation dept
test=>

son1
角色只能查询emp 表的数据,而不能查询dept 表的数据,测试成功。

6.6
查询角色组信息


test=> \c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=#
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB | {}
david | | {}
father | No inheritance | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create role, Create DB | {}
sandy | | {}
son1 | | {father}
son2 | | {father}

postgres=#


Member of ” 项表示son1 和son2 角色属于father 角色组。

七、参考

Postgresql的用户管理二

时间: 2024-07-28 16:06:41

Postgresql的用户管理二的相关文章

Oracle 用户管理(二)

1    给某人赋予"系统权限" SQL> grant connect to aobama with admin option 意思是将admin的连接数据库权限赋予"aobama",并且"aobama"可以将这种权限赋予其他人 2    关于赋予权限收回的问题 描述:A将查询emp表的权限赋予B,B又将该权限赋予C.那么当A把B的该权限收回时,C的权限是否也没有了呢? 答案:SQL> revoke select on emp fro

四、oracle 用户管理二

一.使用profile管理用户口令概述:profile是口令限制,资源限制的命令集合,当建立数据库时,oracle会自动建立名称为default的profile.当建立用户没有指定profile选项时,那么oracle就会将default分配给用户.1.账户锁定概述:指定该账户(用户)登陆时最多可以输入密码的次数,也可以指定用户锁定的时间(天)一般用dba的身份去执行该命令.例子:指定scott这个用户最多只能尝试3次登陆,锁定时间为2天,让我们看看怎么实现.创建profile文件SQL> cr

RHEL7用户管理(二)

RHEL7用户管理 二:用户和组管理命令和配置 1:用户管理命令与实例 1.1:useradd命令 useradd命令用于创建用户,为新用户分配用户号.用户组.主目录和登录Shell等资源,默认情况下只有系统超级用户root才能使用. 语法如下: useradd [-u uid] [-g group] [-G groups] [-d home_dir] [-s shell] [-c comment] [-m [-k shell_dir]] [-N] [-M] login 其中各选项含义如下: -

Linux学习笔记(二)——Linux用户管理和权限管理

Linux系统的权限管理 Linux系统是一个多用户多任务的操作系统,多用户是指系统资源可以被不同用户各自拥有,即每个用户对自己的资源有特定的权限,用户之间互不影响.Linux系统有一套权限管理机制,文件不允许非授权用户访问或修改.这种机制的实现是通过用户和组的形式实现的. 5.1           Linux安全机制 账户管理是Linux安全机制的核心部分.登录Linux系统的用户都会被分配一个的用户账户.用户对系统上文件的访问权限取决于他们登录系统时使用的账户.每个用户的权限是通过创建用户

用户管理命令(二十)

用户管理命令:useradd,usermod,userdel,chage,groupadd,groupdel,groupmod,newgrp,gpasswd 20.1.useradd 功能:添加新用户或更新新用户信息 语法: useradd 选项 用户名 常用参数: -u:用户uid -g:用户的主组 -G:附加组 -s:默认shell -d:家目录 -D:变更预设值 -c:注释说明 -e:过期时间 -f: <缓冲天数> 指定在密码过期后多少天即关闭该账号. -m:自动建立用户的家目录. -M

oracle系列(二)oracle体系结构和用户管理

博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 在使用oracle之前,我们一定要对oracle的体系结构有深入的理解,与之前我们学习过的sqlserver体系结构是不一样的,所以对于我们来说是一个全新的内容. 一.oralce体系结构 1.概述 Oracle的体系结构是数据库的组成,工作过程,以及数据库中数据的组织与管理机制,要了解oracle数据库的体系结构,就必须要理解oracl

mysql(二)-用户管理与权限

用户相关是存放在mysql.user表中,可以使用desc查看表结构 MySQL大小写详情: 1.数据库名严格区分大小写 2.表名严格区分大小写的 3.表的别名严格区分大小写 4.变量名严格区分大小写 5.列名在所有的情况下均忽略大小写 6.列的别名在所有的情况下均忽略大小写 用户管理 用户格式 用户名@可登录主机 [email protected] host:host可以为主机名,也可以为IP地址,mysql里主机名和IP地址属于不同的主机: host可以使用通配符 通配符 代表含义 _ 任意

postgresql用户管理

postgresql用户管理: 默认用户: postgres安装完成后,会自动在操作系统和postgres数据库中分别创建一个名为postgres的用户以及一个同样名为postgres的数据库. 1.组角色: 一个组角色可以看作一组数据库用户.组角色可以拥有数据库对象(比如表),并可以把这些对象上的权限赋予其他角色,以控制谁拥有访问哪些对象的权限. --创建角色: create role role_emp; --查看系统中的角色,如: select rolname from pg_roles;

Linux用户管理命令详解之二

Linux下常用用户管理命令有:useradd.userdel.usermod.passwd.chsh.finger.id.chage 4.改变用户的shell 命令:chsh 语法:chsh [-s shell] [-l] [-u] [-v] [username] 选项: -s:改变当前shell -l:显示/etc/shells目录下的shell 说明:这是用来改变使用者自己的 shell 的指令!由于这个档案能够改变 /etc/passwd 的内容,所以他的预设属性就有SUID的属性了!通