在给业务创建数据库和用户时,应该给予一些基本的业务权限,同时控制系统权限赋予
下面是LightDB或者PostgreSQL基本的权限语句
LightDB或PostgreSQL读写用户权限:
-- Users and Database creation CREATE USER tpch WITH PASSWORD 'tpch'; CREATE DATABASE tpch; ALTER DATABASE tpch OWNER TO tpch; -- Revoke Privileges from PUBLIC REVOKE CREATE ON SCHEMA public FROM PUBLIC; REVOKE ALL ON DATABASE tpch FROM PUBLIC; -- login on tpch ltsql -d tpch create schema tpch; GRANT CONNECT ON DATABASE tpch TO tpch; GRANT USAGE, CREATE ON SCHEMA tpch TO tpch; GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA tpch TO tpch; ALTER DEFAULT PRIVILEGES IN SCHEMA tpch GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO tpch; GRANT USAGE ON ALL SEQUENCES IN SCHEMA tpch TO tpch; ALTER DEFAULT PRIVILEGES IN SCHEMA tpch GRANT USAGE ON SEQUENCES TO tpch; -- Alter schema owner ALTER SCHEMA tpch OWNER TO tpch;
可以看到
tpch@tpch=> select pg_switch_wal(); ERROR: permission denied for function pg_switch_wal tpch@tpch=> checkpoint; ERROR: must be superuser to do CHECKPOINT tpch@tpch=> tpch@tpch=> select pg_terminate_backend(14391); ERROR: must be a superuser to terminate superuser process tpch@tpch=> tpch@tpch=> CREATE USER tpchqueryquery WITH PASSWORD 'tpchqueryquery'; NOTICE: not propagating CREATE ROLE/USER commands to worker nodes HINT: Connect to worker nodes directly to manually create all necessary users and roles. ERROR: permission denied to create role tpch@tpch=> create database haha; ERROR: permission denied to create database tpch@tpch=> create schema test; CREATE SCHEMA
可以看到相应的系统权限和创建用户被屏蔽掉,但是可以在自己的系统内创建schema。
LightDB或PostgreSQL读写用户:
1、在tpch库下的读写权限 -- Users and Database creation CREATE USER tpchquery WITH PASSWORD 'tpchquery'; ALTER DATABASE tpch OWNER TO tpchquery; GRANT USAGE ON SCHEMA tpch TO tpchquery; GRANT SELECT ON ALL TABLES IN SCHEMA tpch TO tpchquery;
如果想要规划为全局的只读用户,也可以配置中自己独立的database
下面是按照此方式配置的只读权限
tpchquery@tpch=> select count(*) from tpch.orders; count -------- 664822 (1 row) tpchquery@tpch=> delete from tpch.orders; ERROR: permission denied for table orders
可以看到tpchquery用户,有tpch库下的tpch shema的只读权限,但却不能操作数据,这是符合预期的