一、角色与用户的区别

角色就相当于岗位:角色可以是经理,助理。

用户就是具体的人:比如陈XX经理,朱XX助理,王XX助理。

在PostgreSQL 里没有区分用户和角色的概念,"CREATE USER" 为 "CREATE ROLE" 的别名,这两个命令几乎是完全相同的,唯一的区别是"CREATE USER" 命令创建的用户默认带有LOGIN属性,而"CREATE ROLE" 命令创建的用户默认不带LOGIN属性(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not)。

1.1 创建角色与用户

CREATE ROLE 语法

CREATE ROLE name [[ WITH] option [...]]where optioncan 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'

| IN ROLE role_name [, ...]

| IN GROUP role_name [, ...]

| ROLE role_name [, ...]

| ADMIN role_name [, ...]

| USER role_name [, ...]

| SYSID uid

创建david 角色和sandy 用户

postgres=# CREATE ROLE david;  //默认不带LOGIN属性CREATEROLE

postgres=# CREATE USER sandy;  //默认具有LOGIN属性CREATEROLE

postgres=# \du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

david | Cannot login |{}

postgres| Superuser, Create role, Create DB, Replication |{}

sandy| |{}

postgres=#

postgres=# SELECT rolname frompg_roles ;

rolname----------

postgres

david

sandy

(3rows)

postgres=# SELECT usename frompg_user; //角色david 创建时没有分配login权限,所以没有创建用户

usename----------

postgres

sandy

(2rows)

postgres=#

1.2 验证LOGIN属性

postgres@CS-DEV:~> psql -U david

psql: FATAL: role "david"is not permitted to log inpostgres@CS-DEV:~> psql -U sandy

psql: FATAL:database "sandy" does notexist

postgres@CS-DEV:~> psql -U sandy -d postgres

psql (9.1.0)

Type "help"forhelp.

postgres=>\dt

No relations found.

postgres=>

用户sandy 可以登录,角色david 不可以登录。

1.3 修改david 的权限,增加LOGIN权限

postgres=# ALTERROLE david LOGIN ;ALTERROLE

postgres=# \du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

david | |{}

postgres| Superuser, Create role, Create DB, Replication |{}

sandy| |{}

postgres=# SELECT rolname frompg_roles ;

rolname----------

postgres

sandy

david

(3rows)

postgres=# SELECT usename frompg_user;  //给david 角色分配login权限,系统将自动创建同名用户david

usename----------

postgres

sandy

david

(3rows)

postgres=#

1.4 再次验证LOGIN属性

postgres@CS-DEV:~> psql -U david -d postgres

psql (9.1.0)

Type "help"forhelp.

postgres=>\du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

david | |{}

postgres| Superuser, Create role, Create DB, Replication |{}

sandy| |{}

postgres=>

david 现在也可以登录了。

二、查看角色信息

psql 终端可以用\du 或\du+ 查看,也可以查看系统表 select * from pg_roles;

postgres=>\du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

david | Cannot login |{}

postgres| Superuser, Create role, Create DB, Replication |{}

sandy| |{}

postgres=> \du+Listofroles

Role name| Attributes | Member of |Description-----------+------------------------------------------------+-----------+-------------

david | Cannot login | {} |postgres| Superuser, Create role, Create DB, Replication | {} |sandy| | {} |postgres=> SELECT * frompg_roles;

rolname| rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig |oid----------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+---------------+-----------+-------

postgres | t | t | t | t | t | t | t | -1 | ******** | | | 10david| f | t | f | f | f | f | f | -1 | ******** | | | 49438sandy| f | t | f | f | f | t | f | -1 | ******** | | | 49439(3rows)

postgres=>

三、角色属性(Role Attributes)

一个数据库角色可以有一系列属性,这些属性定义了他的权限。

属性

说明

login

只有具有 LOGIN 属性的角色可以用做数据库连接的初始角色名。

superuser

数据库超级用户

createdb

创建数据库权限

createrole

允许其创建或删除其他普通的用户角色(超级用户除外)

replication

做流复制的时候用到的一个用户属性,一般单独设定。

password

在登录时要求指定密码时才会起作用,比如md5或者password模式,跟客户端的连接认证方式有关

inherit

用户组对组员的一个继承标志,成员可以继承用户组的权限特性

...

...

四、创建用户时赋予角色属性

从pg_roles 表里查看到的信息,在上面创建的david 用户时,默认没有创建数据库等权限。

postgres@CS-DEV:~> psql -U david -d postgres

psql (9.1.0)

Type "help"forhelp.

postgres=>\du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

david | |{}

postgres| Superuser, Create role, Create DB, Replication |{}

sandy| |{}

postgres=> CREATE DATABASEtest;

ERROR: permission deniedto create databasepostgres=>

如果要在创建角色时就赋予角色一些属性,可以使用下面的方法。

首先切换到postgres 用户。

4.1 创建角色bella 并赋予其CREATEDB 的权限。

postgres=# CREATEROLE bella CREATEDB ;CREATEROLE

postgres=# \du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

bella | Create DB, Cannot login |{}

david| |{}

postgres| Superuser, Create role, Create DB, Replication |{}

sandy| |{}

postgres=#

4.2 创建角色renee 并赋予其创建数据库及带有密码登录的属性。

postgres=# CREATE ROLE renee CREATEDB PASSWORD 'abc123'LOGIN;CREATEROLE

postgres=# \du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

bella | Create DB, Cannot login |{}

david| |{}

postgres| Superuser, Create role, Create DB, Replication |{}

renee| Create DB |{}

sandy| |{}

postgres=#

4.3 测试renee 角色

a. 登录

postgres@CS-DEV:~> psql -U renee -d postgres

psql (9.1.0)

Type "help"forhelp.

postgres=>

用renee 用户登录数据库,发现不需要输入密码既可登录,不符合实际情况。

b. 查找原因

在角色属性中关于password的说明,在登录时要求指定密码时才会起作用,比如md5或者password模式,跟客户端的连接认证方式有关。

查看pg_hba.conf 文件,发现local 的METHOD 为trust,所以不需要输入密码。

将local 的METHOD 更改为password,然后保存重启postgresql。

c. 再次验证

提示输入密码,输入正确密码后进入到数据库。

d. 测试创建数据库

创建成功。

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

使用ALTER ROLE 命令。

ALTER ROLE 语法:

ALTER ROLE name [[ WITH] option [...]]where optioncan 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 TOnew_nameALTER 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_parameterALTER ROLE name [IN DATABASE database_name] RESET ALL

5.1 赋予bella 登录权限

a. 查看现在的角色属性

postgres=# \du

Listofroles

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 WITHLOGIN;ALTERROLE

postgres=# \du

Listofroles

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 WITHCREATEROLE;ALTERROLE

postgres=# \du

Listofroles

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';ALTERROLE

postgres=#

5.4 设置sandy 角色的有效期

postgres=# ALTER ROLE sandy VALID UNTIL '2014-04-24';ALTERROLE

postgres=# \du

Listofroles

Role name| Attributes | Member of

-----------+------------------------------------------------+-----------

bella | Create DB |{}

david| |{}

postgres| Superuser, Create role, Create DB, Replication |{}

renee| Create role, Create DB |{}

sandy| |{}

postgres=# SELECT * frompg_roles ;

rolname| rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig |oid----------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+------------------------+-----------+-------

postgres | t | t | t | t | t | t | t | -1 | ******** | | | 10bella| f | t | f | t | f | t | f | -1 | ******** | | | 49440renee| f | t | t | t | f | t | f | -1 | ******** | | | 49442david| f | t | f | f | f | t | f | -1 | ******** | | | 49438sandy| f | t | f | f | f | t | f | -1 | ******** | 2014-04-24 00:00:00+08 | | 49439(5rows)

postgres=#

六、角色赋权/角色成员

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

6.1 创建组角色

postgres=# CREATE ROLE father login nosuperuser nocreatedb nocreaterole noinherit encrypted password 'abc123';CREATEROLE

postgres=# \du

Listofroles

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 tofather;GRANTpostgres=# \c test renee

You are now connectedto database "test" as user"renee".

test=>\dt

No relations found.

test=> CREATE TABLEemp (

test(>id serial,

test(> name text);

NOTICE:CREATE TABLE will create implicit sequence "emp_id_seq" for serial column"emp.id"CREATE TABLEtest=> INSERT INTO emp (name) VALUES ('david');INSERT 0 1test=> INSERT INTO emp (name) VALUES ('sandy');INSERT 0 1test=> SELECT * fromemp;

id|name----+-------

1 |david2 |sandy

(2rows)

test=>\dt

ListofrelationsSchema | Name | Type |Owner--------+------+-------+-------

public | emp | table |renee

(1row)

test=> GRANT USAGE ON SCHEMA public tofather;

WARNING: noprivileges were granted for "public"GRANTtest=> GRANT SELECT on public.emp tofather;GRANTtest=>

6.3 创建成员角色

test=>\c postgres postgres

You are now connectedto database "postgres" as user"postgres".

postgres=# CREATE ROLE son1 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'abc123';CREATEROLE

postgres=#

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

6.4 将father 角色赋给son1

postgres=# GRANT father toson1;GRANTROLE

postgres=#

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

postgres=# CREATE ROLE son2 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'abc123' inrole father;CREATEROLE

postgres=#

6.5 测试son1 角色

postgres=# \c test son1

You are now connectedto database "test" as user"son1".

test=>\dt

ListofrelationsSchema | Name | Type |Owner--------+------+-------+-------

public | emp | table |renee

(1row)

test=> SELECT * fromemp;

id|name----+-------

1 |david2 |sandy

(2rows)

test=>

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

test=>\c test renee

You are now connectedto database "test" as user"renee".

test=> CREATE TABLEdept (

test(> deptid integer,

test(> deptname text);CREATE TABLEtest=> INSERT INTO dept (deptid, deptname) values(1, 'ts');INSERT 0 1test=>\c test son1

You are now connectedto database "test" as user"son1".

test=> SELECT * fromdept ;

ERROR: permission deniedforrelation dept

test=>

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

6.6 查询角色组信息

test=>\c postgres postgres

You are now connectedto database "postgres" as user"postgres".

postgres=#

postgres=# \du

Listofroles

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 角色组。

七、参考

查看pg 用户组_PostgreSQL 角色管理相关推荐

  1. 查看pg 用户组_PostgreSQL 角色用户管理

    一.角色与用户的区别 PostgreSQL使用角色的概念管理数据库访问权限. 根据角色自身的设置不同,一个角色可以看做是一个数据库用户,或者一组数据库用户. 角色可以拥有数据库对象(比如表)以及可以把 ...

  2. mysql8.0查看用户_MySQL 8.0用户和角色管理

    MySQL8.0新加了很多功能,其中在用户管理中增加了角色的管理, 默认的密码加密方式也做了调整,由之前的sha1改为了sha2,同时加上5.7的禁用用户和用户过期的设置, 这样方面用户的管理和权限的 ...

  3. 有关Oracle角色管理

    一. 系统权限 1.授予系统权利 Sql>connect / as sysdba Sql>grant create session to user1: Sql>grant creat ...

  4. Linux系统之高级用户组和权限管理

    Linux系统之高级用户组和权限管理 一.用户的密码策略设置 1.用户的密码文件 2.用户的密码期限配置 ①查看用户密码期限 ②修改密码期限 ③强制用户下一次修改密码 ④用户到期时间设置 3.查看当前 ...

  5. SpringMvc 集成 shiro 实现权限角色管理-maven

    2019独角兽企业重金招聘Python工程师标准>>> SpringMvc 集成 shiro 实现权限角色管理 1.项目清单展示 2.项目源码解析  1)spring-context ...

  6. orale用户角色管理

    Oracle 权限设置 一.权限分类: 系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对表或视图而言的).   二.系 ...

  7. RDIFramework.NET ━ 9.4 角色管理 ━ Web部分

    RDIFramework.NET ━ 9.4 角色管理 ━ Web部分 RDIFramework.NET ━ .NET快速信息化系统开发框架 9.4 角色管理 -Web部分 角色管理模块主要为了方便框 ...

  8. 【.NETCore 3】Ids4 ║ 统一角色管理(上)

    前言 书接上文,咱们在上周,通过一篇<思考> 性质的文章,和很多小伙伴简单的讨论了下,如何统一同步处理角色的问题,众说纷纭,这个我一会儿会在下文详细说到,而且我最终也定稿方案了.所以今天咱 ...

  9. 【数据库】Oracle用户、授权、角色管理

    创建和删除用户是Oracle用户管理中的常见操作,但这其中隐含了Oracle数据库系统的系统权限与对象权限方面的知识.掌握还Oracle用户的授权操作和原理,可以有效提升我们的工作效率. Oracle ...

最新文章

  1. 基于STC8G8K64U三通道高速ADC采集板
  2. mysql 返回最大值列名_多列求最大值列和列名
  3. Python 随机森林分类
  4. 【SDOI 2011】Paint 染色
  5. python下载图片的命令_网上的图片不知道怎么批量下载?python教你怎么把网站上面的图片都爬下来...
  6. Ubuntu中apt与apt-get命令的区别
  7. pg 事务 存储过程_PgpoolII实现数据分区存储及性能分析
  8. 深度学习2.0-38.RNNCell使用-RNN Layer
  9. ztree Api官方文档
  10. 函数分离常数法 oracle,2009届高三数学第一轮复习课件:函数(最新)幻灯片
  11. 培训Java程序员技术真的差吗?
  12. cortana连不上网络_Alexa,为什么Cortana仍在我的计算机上?
  13. 相亲婚恋网站哪个好!交友、约会、找对象都用靠谱的一伴网!
  14. Nature Genetics:华中农业大学严建兵团队合作揭示玉米和玉米的“父辈”如何适应环境进化...
  15. 为上次渲染的三角形添加颜色
  16. WPS表格级联菜单设置方法
  17. vscode的leetcode插件无法账号登陆([ERROR] Login failed. Please make sure the credential is correct)
  18. Unity程序化地形教程集合
  19. java 对接 stripe支付
  20. Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Lighting - 01 - Colors

热门文章

  1. WinCE6.0的极速启动
  2. C#对Microsoft.VisualBasic My对象兰台妙选
  3. Boson NetSim实验模拟器破解
  4. [ZT]恐怖“标语”
  5. java中String值为空字符串与null的判断方法
  6. Java中判断String不为空的问题
  7. Python的reshape(-1,1)
  8. lnmp1.4上thinkphp5.0出现404的解决办法
  9. PHP算法用redis crontab 进行异步邮件队列发送
  10. PHP算法导出Excel实现字段联动