位置: 编程技术 - 正文

PostgreSQL 角色与用户管理介绍

编辑:rootadmin

一、角色与用户的区别角色就相当于岗位:角色可以是经理,助理。用户就是具体的人:比如陈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 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' | 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属性CREATE ROLEpostgres=# CREATE USER sandy;  //默认具有LOGIN属性CREATE ROLEpostgres=# du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- david | Cannot login | {} postgres | Superuser, Create role, Create DB, Replication | {} sandy | | {}

postgres=# postgres=# SELECT rolname from pg_roles ; rolname ---------- postgres david sandy(3 rows)

postgres=# SELECT usename from pg_user; //角色david 创建时没有分配login权限,所以没有创建用户 usename ---------- postgres sandy(2 rows)

postgres=# 1.2 验证LOGIN属性postgres@CS-DEV:~> psql -U davidpsql: FATAL: role "david" is not permitted to log inpostgres@CS-DEV:~> psql -U sandypsql: FATAL: database "sandy" does not existpostgres@CS-DEV:~> psql -U sandy -d postgrespsql (9.1.0)Type "help" for help.

postgres=> dtNo relations found.postgres=> 用户sandy 可以登录,角色david 不可以登录。1.3 修改david 的权限,增加LOGIN权限postgres=# ALTER ROLE david LOGIN ;ALTER ROLEpostgres=# du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- david | | {} postgres | Superuser, Create role, Create DB, Replication | {} sandy | | {}

postgres=# SELECT rolname from pg_roles ; rolname ---------- postgres sandy david(3 rows)

postgres=# SELECT usename from pg_user;  //给david 角色分配login权限,系统将自动创建同名用户david usename ---------- postgres sandy david(3 rows)

postgres=# 1.4 再次验证LOGIN属性postgres@CS-DEV:~> psql -U david -d postgrespsql (9.1.0)Type "help" for help.

postgres=> du List of roles 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 List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- david | Cannot login | {} postgres | Superuser, Create role, Create DB, Replication | {} sandy | | {}

postgres=> du+ List of roles Role name | Attributes | Member of | Description -----------+------------------------------------------------+-----------+------------- david | Cannot login | {} | postgres | Superuser, Create role, Create DB, Replication | {} | 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 | ******** | | | david | f | t | f | f | f | f | f | -1 | ******** | | | sandy | f | t | f | f | f | t | f | -1 | ******** | | | (3 rows)

postgres=>

三、角色属性(Role Attributes)

推荐整理分享PostgreSQL 角色与用户管理介绍,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

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

属性说明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" for help. postgres=> du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- david | | {} postgres | Superuser, Create role, Create DB, Replication | {} sandy | | {} postgres=> CREATE DATABASE test; ERROR: permission denied to create database postgres=> 如果要在创建角色时就赋予角色一些属性,可以使用下面的方法。 首先切换到postgres 用户。 4.1 创建角色bella 并赋予其CREATEDB 的权限。 postgres=# CREATE ROLE bella CREATEDB ; CREATE ROLE postgres=# du List of roles 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 'abc' LOGIN; CREATE ROLE 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=# 4.3 测试renee 角色 a. 登录 postgres@CS-DEV:~> psql -U renee -d postgres psql (9.1.0) Type "help" for help. postgres=> 用renee 用户登录数据库,发现不需要输入密码既可登录,不符合实际情况。 b. 查找原因 在角色属性中关于password的说明,在登录时要求指定密码时才会起作用,比如md5或者password模式,跟客户端的连接认证方式有关。

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

PostgreSQL 角色与用户管理介绍

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

c. 再次验证

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

d. 测试创建数据库

创建成功。

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

使用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 CURRENTALTER ROLE name [ IN DATABASE database_name ] RESET configuration_parameterALTER ROLE name [ IN DATABASE database_name ] RESET ALL5.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 ROLEpostgres=# 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 ROLEpostgres=# 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 'ufo';ALTER ROLEpostgres=#5.4 设置sandy 角色的有效期postgres=# ALTER ROLE sandy VALID UNTIL '--';ALTER ROLEpostgres=# 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 | ******** | | | bella | f | t | f | t | f | t | f | -1 | ******** | | | renee | f | t | t | t | f | t | f | -1 | ******** | | | david | f | t | f | f | f | t | f | -1 | ******** | | | sandy | f | t | f | f | f | t | f | -1 | ******** | -- ::+ | | (5 rows)

postgres=#

六、角色赋权/角色成员

在系统的角色管理中,通常会把多个角色赋予一个组,这样在设置权限时只需给该组设置即可,撤销权限时也是从该组撤销。在PostgreSQL中,首先需要创建一个代表组的角色,之后再将该角色的membership 权限赋给独立的角色即可。6.1 创建组角色postgres=# CREATE ROLE father login nosuperuser nocreatedb nocreaterole noinherit encrypted password 'abc';CREATE ROLEpostgres=# 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;GRANTpostgres=# c test reneeYou are now connected to database "test" as user "renee".test=> dtNo 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 TABLEtest=> INSERT INTO emp (name) VALUES ('david'); INSERT 0 1test=> INSERT INTO emp (name) VALUES ('sandy');INSERT 0 1test=> SELECT * from emp; id | name ----+------- 1 | david 2 | 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"GRANTtest=> GRANT SELECT on public.emp to father;GRANTtest=> 6.3 创建成员角色test=> c postgres postgresYou are now connected to database "postgres" as user "postgres".postgres=# CREATE ROLE son1 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'abc';CREATE ROLEpostgres=# 这里创建了son1 角色,并开启inherit 属性。PostgreSQL 里的角色赋权是通过角色继承(INHERIT)的方式实现的。6.4 将father 角色赋给son1postgres=# GRANT father to son1;GRANT ROLEpostgres=# 还有另一种方法,就是在创建用户的时候赋予角色权限。postgres=# CREATE ROLE son2 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'abc' in role father;CREATE ROLEpostgres=# 6.5 测试son1 角色postgres=# c test son1You 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 ----+------- 1 | david 2 | sandy(2 rows)

test=> 用renee 角色新创建一张表,再次测试test=> c test reneeYou are now connected to database "test" as user "renee".test=> CREATE TABLE dept (test(> deptid integer,test(> deptname text);CREATE TABLEtest=> INSERT INTO dept (deptid, deptname) values(1, 'ts');INSERT 0 1test=> c test son1You are now connected to database "test" as user "son1".test=> SELECT * from dept ;ERROR: permission denied for relation depttest=> son1 角色只能查询emp 表的数据,而不能查询dept 表的数据,测试成功。6.6 查询角色组信息test=> c postgres postgresYou 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 查看数据库,索引,表,表空间大小的示例代码 一、简介PostgreSQL提供了多个系统管理函数来查看表,索引,表空间及数据库的大小,下面详细介绍一下。二、数据库对象尺寸函数函数名返回类型描述p

修改一行代码提升 Postgres 性能 倍 在一个(差)的PostgreSQL查询中只要一个小小到改动(ANY(ARRAY[...])toANY(VALUES(...)))就能把查询时间从s缩减到0.2s。从最简单的学习使用EXPLAINANALYZE开始,到

Windows下PostgreSQL安装图解 现在谈起免费数据库,大多数人首先想到的可能是MySQL,的确MySQL目前已经应用在国内很多领域,尤其是网站架设方面。但是,实际上功能最强大、特性

标签: PostgreSQL 角色与用户管理介绍

本文链接地址:https://www.jiuchutong.com/biancheng/319653.html 转载请保留说明!

上一篇:PostgreSQL中的OID和XID 说明

下一篇:PostgreSQL 查看数据库,索引,表,表空间大小的示例代码

  • 企业所得税季度申报表怎么填
  • 运输发票税率是9
  • 收据盖发票专用章后果
  • 利润表利息费用和利息收入
  • 增值税专用发票的三流合一是什么意思
  • 工会筹备金和工会经费一样吗
  • 一般纳税人建筑工程税率是多少
  • 附加税减半征收会计分录
  • 小规模纳税人零申报要报哪些税
  • 一般纳税人什么时候用简易计税
  • 预收款结转收入怎么算
  • 离退休人员的返乡政策
  • 总公司和分公司类型必须一样么
  • 个人设备卖给公司
  • 一般纳税人每个月几号报税
  • 可交换债券与可转换债券的区别在于
  • 采购商品未入库的会计分录
  • 行政事业单位餐标
  • 暂估入库冲回的会计分录
  • 联营扣点方案
  • 购买日子公司账面价值调整为公允价值
  • 计提固定资产减值准备属于什么支出
  • 个人去税务局开居间费发票
  • 企业所得所得税税负
  • 税控维护费的分录
  • 土地使用税级别划分标准
  • 借款利息可以入账吗
  • 银行承兑贴息率是年利率吗
  • 增值税包括土地整治收入吗
  • 建安行业简易计税可以抵扣吗
  • 所得税a类申报表
  • 总成本费用包含
  • 公司股权分红款需要税吗
  • 计提劳务派遣人员社保收到发票后没有付款的会计分录
  • 路由器隐藏ssid后果严重
  • 如果退货卖家拒绝会把货退回来么
  • 房地产企业出售土地交什么税
  • 工业企业会计核算的特点
  • 跟银行借入长期存款
  • linux入门学习
  • 巴黎先贤祠的名言
  • 投资性房地产的主要构成内容为
  • 进项税额转出可以填负数吗
  • 股东出资未注明投资款可以通过验资报告处理么
  • 增值税税控系统专用设备费及技术维护费抵扣
  • 升级nodejs到最新版本
  • 周志华《机器学习》第三章课后习题
  • vue3 响应式ui框架
  • 2022年微信小程序游戏
  • 房屋租赁费属于什么税收分类编码
  • 公司账户的资金有利息吗
  • 建筑业为什么要报统计局
  • 固定资产科目的期末余额,反映固定资产的原价
  • 当月进项税大于销项税怎么办
  • 供应商费用是什么
  • 一般纳税人开具的普票可以抵扣吗
  • 出差补助是否计入工资总额
  • 什么是现金什么是现金流
  • 公司股权变更如何合理避税
  • 租出的固定资产
  • 土地储备委员会职责
  • 预缴所得税年底怎么算
  • 残疾基金缴纳标准
  • sql排序分组
  • mysql分表命令
  • winxp和win7双系统安装
  • Win10 Mobile RS2预览版14951升级遭遇卡在0%的解决办法
  • linux系统
  • RAVMOND.exe - RAVMOND是什么进程.有什么作用
  • win8.1介绍
  • windows10快速操作在哪
  • unity learn premium
  • javascript基础入门视频教程
  • jquery使用教程
  • android实时获取微信聊天信息
  • 安卓权限管理app
  • 车辆退款
  • 柬埔寨现在的领导
  • 年度关联业务往来报告
  • 消费税的税率只有比例税率和定额税率两种判断题
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设