highgo=# grant usage on schema test_schema to read_only ; GRANT
(2)然后加所有表的只读权限:
highgo=# grant select on all tables in schema test_schema to read_only; GRANT 如果不想给所有表的查询权限,则单独给某个表的查询权限: highgo=# grant select on TABLE test_schema.abc to read_only; GRANT
4、可以进行一下权限测试:
[highgo@localhost ~]$ psql -U read_only highgo highgo=> select * from test_schema.abc limit 1; a | b ---+---------------------------------- 1 | db18340e7e9a86ea85a64addd9ea309f (1 row)
highgo=> insert into test_schema.abc values(10,'10'); ERROR: 25006: cannot execute INSERT in a read-only transaction highgo=> delete from test_schema.abc where a=1; ERROR: 25006: cannot execute DELETE in a read-only transaction highgo=> update test_schema.abc set b = 'xx' where a = 1; ERROR: 25006: cannot execute UPDATE in a read-only transaction
5、如果要在别的数据库访问:
(1) 先要用highgo(超级用户登录),然后\c到对应的数据库:
highgo=# \c test PSQL: Release 5.6.4 Connected to: HighGo Database V5.6 Enterprise Edition Release 5.6.4 - 64-bit Production You are now connected to database "test" as user "highgo".
(2) 执行下面的命令,将对应的schema的表查询权限给这个用户:
# test数据库的public模式的usage权限是默认就有的,只需要添加表的只读权限即可: test=# grant select on all tables in schema public to read_only; GRANT