AWS - Redshift - 数据库 schema 表 之间的关系

关于Redshift 中不同库之间的关系。以及schema 之间的关系。


-- 准备工作:
-- 使用master user
-- 建DB, db1, db2
-- 在db1中和db2中 建Schema, haha
-- 建用户, u1, u2
testdb=# create database db1;
CREATE DATABASE
testdb=# create database db2;
CREATE DATABASE
testdb=# \c db1;
psql (12.2, server 8.0.2)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
You are now connected to database "db1" as user "dbadmin".
db1=# create user u1 password 'password';
CREATE USER
db1=# create user u2 password 'password';
CREATE USER
db1=# create schema haha;
CREATE SCHEMA

-- 在 db2 中创建 schema haha下的表,不成功。
db2=# create table haha.t_dbadmin_db2_haha;
ERROR:  schema "haha" does not exist

-- 使用u1 登录 db1
-- 未进行任何授权
-- 无法创建 schema haha下的表
You are now connected to database "db1" as user "u1".
db1=> create table t_u1_public(id int);
CREATE TABLE
db1=> create table haha.t_u1_haha(id int);
ERROR:  permission denied for schema haha


-- 使用 u2 登录 db1
-- 未进行任何授权
-- 无法创建 schema haha下的表
-- 无法访问 u1 创建的表
You are now connected to database "db1" as user "u2".
db1=> create table t_u2_public(id int);
CREATE TABLE
db1=> create table haha.t_u2_haha(id int);
ERROR:  permission denied for schema haha
db1=> select * from t_u1_public ;
ERROR:  permission denied for relation t_u1_public

-- 使用 
You are now connected to database "db2" as user "dbadmin".
db2=# create schema haha;
CREATE SCHEMA

-- 使用master user 在 db2 中给用户授权 schema haha
db2=# grant all on SCHEMA haha to u1;
GRANT
db2=# grant all on SCHEMA haha to u2;
GRANT

-- u1 分别在db1, db2 中的schema haha 下建表
-- db1 中建表不成功
-- db2 中建表成功
db1=> create table haha.t_u1_db1_haha(id int);
ERROR:  permission denied for schema haha
You are now connected to database "db1" as user "u1".
db2=> create table haha.t_u1_db2_haha(id int);
CREATE TABLE

-- 使用master user在 db1 中给用户授权 schema haha
You are now connected to database "db1" as user "dbadmin".
db1=# grant all on SCHEMA haha to u1;
GRANT
db1=# grant all on SCHEMA haha to u2;
GRANT
-- u1 在 t1 中建表成功
You are now connected to database "db1" as user "u1".
db1=> create table haha.t_u1_db1_haha(id int);
CREATE TABLE

-- 在 db1 中 通过 u2 查看 u1 的表
You are now connected to database "db1" as user "u2".
db1=> select * from haha.t_u1_db1_haha ;
ERROR:  permission denied for relation t_u1_db1_haha

-- 在 db1 中通过 masteruser 给 u2 授权。 
You are now connected to database "db1" as user "dbadmin".
db1=# grant all ON all tables in schema haha to u2;
GRANT

-- 在db1 中通过 u2 查询 u1 表成功
You are now connected to database "db1" as user "u2".
db1=> select * from haha.t_u1_db1_haha ;
 id
----
(0 rows)


-- 通过 u1 在 db2 中建立新表
You are now connected to database "db2" as user "u1".
db2=> create table haha.t_u1_db1_haha(id int);
CREATE TABLE

-- 使用u2 在 db2 中 访问u1在schema haha下的表,不成功
db2=> select * from haha.t_u1_db1_haha;
ERROR:  permission denied for relation t_u1_db1_haha

-- 通过 masteruser 授权
db2=# grant all ON all tables in schema haha to u2;
GRANT

-- 通过 u2 再次访问 u1 的表,成功
db2=> select * from haha.t_u1_db1_haha;
 id
----
(0 rows)


如何查看表的权限

-- 查看表权限
select * from information_schema.table_privileges where table_name = 'table'; -- 替换您需要查看的 table name

-- 查看 用户 与 schema 之间的权限关系
SELECT
    u.usename,
    s.schemaname,
    has_schema_privilege(u.usename,s.schemaname,'create') AS user_has_select_permission,
    has_schema_privilege(u.usename,s.schemaname,'usage') AS user_has_usage_permission
FROM
    pg_user u
CROSS JOIN
    (SELECT DISTINCT schemaname FROM pg_tables) s
WHERE
    u.usename = 'UserName'
    AND s.schemaname = 'SchemaName'; -- 替换您的 用户名 和 schema 名称

你可能感兴趣的:(AWS-Redshift)