postgresql限制某个用户仅连接某一个数据库的操作
创建数据库bbb且owner为用户b:
postgres9.6@[local]:5432postgres#createdatabasebbbownerb; CREATEDATABASE Time:259.099ms
默认情况下使用用户c也可以连接数据库bbb:
postgres9.6@[local]:5432postgres#\cbbbc Youarenowconnectedtodatabase"bbb"asuser"c".
回收public的connectondatabasebbb权限:
c@[local]:5432bbb#\cpostgrespostgres9.6 Youarenowconnectedtodatabase"postgres"asuser"postgres9.6". postgres9.6@[local]:5432postgres#revokeconnectondatabasebbbfrompublic; REVOKE Time:2.088ms
此时用户c没有了连接数据库bbb的权限:
postgres9.6@[local]:5432postgres#\cbbbc FATAL:permissiondeniedfordatabase"bbb" DETAIL:UserdoesnothaveCONNECTprivilege. Previousconnectionkept
但bbb数据库的owner用户b可以连接数据库:
postgres9.6@[local]:5432postgres#\cbbbb Youarenowconnectedtodatabase"bbb"asuser"b". b@[local]:5432bbb#
此种情况下超级用户也可以连接该数据库:
b@[local]:5432bbb#\cbbbpostgres9.6
Youarenowconnectedtodatabase"bbb"asuser"postgres9.6".
postgres9.6@[local]:5432bbb#
postgres9.6@[local]:5432bbb#\du
Listofroles
Rolename|Attributes|Memberof
-------------+------------------------------------------------------------+-----------
a||{}
b||{}
c||{}
postgres9.6|Superuser,Createrole,CreateDB,Replication,BypassRLS|{}
另外一种方法:从pg_hba.conf中限定:
#TYPEDATABASEUSERADDRESSMETHOD #"local"isforUnixdomainsocketconnectionsonly localallalltrust #IPv4localconnections: hostallall127.0.0.1/32trust ... # hostallall0.0.0.0/0md5
补充:Postgres限制每个用户只能连接指定数量的session,防止服务器资源紧张
限制每个用户只能连接指定数量的session,防止服务器资源紧张
(1)创建测试用户test:
highgo=#createusertest;
CREATEROLE
highgo=#\du
Listofroles
Rolename|Attributes|Memberof
-----------+------------------------------------------------+----------
highgo|Superuser,Createrole,CreateDB,Replication|{}
test||{}
(2)设置仅允许用户test使用一个连接
highgo=#ALTERROLEtestCONNECTIONLIMIT1; ALTERROLE
(3)在session1中使用test用户连接highgo数据库
highgo=>\chighgotest Youarenowconnectedtodatabase"highgo"asuser"test". highgo=>
(4)在session2中也使用test用户连接highgo数据库,会出现如下错误:
highgo=#\chighgotest 致命错误:由角色"test"发起的连接太多了 Previousconnectionkept
(5)查询用户test链接限制
highgo=>SELECTrolconnlimitFROMpg_rolesWHERErolname='test'; rolconnlimit -------------- 1 (1row)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下