postgres赋予角色登录权限

来源:互联网 发布:淘宝男士休闲鞋 编辑:程序博客网 时间:2024/06/10 03:53
创建的role默认没有登录数据库的权限:
highgo=# create role a with password 'aaa';
CREATE ROLE
highgo=# \c highgo a
致命错误:  不允许角色"a" 进行登录

需要单独赋予角色login权限:
highgo=# alter role a login;
ALTER ROLE
highgo=# \c highgo a
You are now connected to database "highgo" as user "a".
highgo=> 

直接创建的user可以登录数据库:
highgo=# create user b with password 'bbb';
CREATE ROLE
highgo=# \c highgo b
You are now connected to database "highgo" as user "b".
highgo=> 

*****************************************************************
将角色赋予login权限后,可以连接登录所有的数据库:
highgo=# alter role a login;
ALTER ROLE
highgo=# \c highgo a
You are now connected to database "highgo" as user "a".
highgo=> \c template1 a
You are now connected to database "template1" as user "a".
template1=> \c
You are now connected to database "template1" as user "a".
template1=> \c highgo highgo
You are now connected to database "highgo" as user "highgo".

回收角色的login权限后,赋予连接某一个数据库的权限后也无法连接数据库:
highgo=# alter role a nologin;
ALTER ROLE
highgo=# grant connect on database template1 to a;
GRANT
highgo=# \c template1 a
致命错误:  不允许角色"a" 进行登录
Previous connection kept

*************************************************************************
highgo=# \c benchmarksql b
致命错误:  不允许角色"b" 进行登录
Previous connection kept
highgo=# grant connect on database benchmarksql to b;
GRANT
highgo=# \c benchmarksql b
致命错误:  不允许角色"b" 进行登录
Previous connection kept
highgo=# alter role b login;
ALTER ROLE
highgo=# \c benchmarksql b
You are now connected to database "benchmarksql" as user "b".
benchmarksql=> \c highgo highgo
You are now connected to database "highgo" as user "highgo".
highgo=# drop role b;
错误:  无法删除"b"因为有其它对象倚赖它
DETAIL:  数据库 benchmarksql的权限
highgo=# drop role b cascade;
错误:  语法错误 在 "cascade" 或附近的
LINE 1: drop role b cascade;
                    ^
highgo=# revoke connect on database benchmarksql from b;
REVOKE
highgo=# drop role b ;
DROP ROLE