PostgreSql数据库创建用户并授权
postgreSql创建用户并授权
postgreSql创建用户并授权
创建只读用户,控制用户访问权限
-
创建数据为
CREATE DATABASE data_transfer; -
pg 默认所有用户都可以在名称为
public的schema中创建表,只读用户不允许创建,所以要收回此权限revoke create on schema public from public; -
创建schmea
create schema data_out -
创建只读用户
readonlycreate user readonly with password 'password'; -
授权用户使用
data_out权限grant usage on schema data_out to readonly; -
授权用户查看
schema data_out下的所有表(此时库的的所有表,新增表不能看到)grant select on all tables in schema data_out to readonly; -
授权
readonly用户查看新创建表的select权限alter default privileges in schema data_out grant select on tables to readonly; -
授予指定
schema下所有数据表及序列的权限
grant all privileges on all tables in schema public to tkk123;
grant all privileges on all sequences in schema public to tkk123;
注意:上面的授权可能只对历史的一些对象授权,后期增加的对象是没有权限的,需要给个默认权限
-
赋予默认数据表的权限
alter default privileges in schema public grant all privileges on tables to tkk123; -
赋予默认序列的权限
alter default privileges in schema public grant all privileges on sequences to tkk123;
在指定数据为执行,回收连接public权限,防止已经创建的库未设置权限,表被访问到
revoke connect on database rohs from public;
schema操作
- 创建
schema指定owner
默认是谁创建的schema,owner就是谁,可以通过如下方式进行指定
hadoop=# create schema schema_data_out authorization readonly;
CREATE SCHEMA
hadoop=# create schema authorization sha;
CREATE SCHEMA
hadoop=# \dn
List of schemas
Name | Owner
---------------------+----------
sha | sha
public | postgres
schema_data_out | readonly
(3 rows)
指定了
owner,不指定schema,则schema名字与owner一致。
删除用户
# 将old_user拥有owner权限的对象转移给other_user,那么old_user将不会再拥有owner权限的对象了
# REASSIGN OWNED BY中可以将对象的拥有者修改为postgres或是其他较高权限的业务账号
REASSIGN OWNED BY old_user TO postgres;
# 在当前库中,删除用户拥有的对象;并收回在当前库赋予的任何权限
# 由于之前已经使用了REASSIGN OWNED BY,old_user用户下已无owned的对象,对于账号不只是存在owner的情况,可能用户只是查询或修改的权限,此时再使用DROP OWNED BY则可以将其他权限收回。
DROP OWNED BY old_user;
# 最后删除账号
DROP USER old_user;
注意:
REASSIGN OWNED BY和DROP OWNED BY命令只能删除对应库中的权限,如果在其他库中仍有要收回的权限,要切换到其他库执行。
更多推荐

所有评论(0)