개요
- superuser가 아닌 사용자가 데이터베이스를 사용하려면 특정 권한들이 부여되어야 한다. (schema의 usage권한, table select 권한 등)
- PostgreSQL에서 지원하는 권한 확인 방법들은 한눈에 보기에 불편하다. 그래서 보기 편하도록 만든 여러 SQL문들이다.
내장테이블을 통한 확인
- 몇몇 postgres 내장 테이블들에서 권한 부여 상황을 확인할 수 있다.
확인 가능 테이블
- 테이블 권한 확인
information_schema.role_table_grants
information_schema.table_privileges
- 스키마 권한 확인
information_schema.schemata
Check privilege - user중심
- user 중심으로 권한 상황을 볼 수 있는 TABLE을 만들어주는 SQL문들이다.
schema 확인
select pgu.usename as user_name,
(select string_agg(scl.schema_name, ',' order by scl.schema_name)
from information_schema.schemata as scl
where has_schema_privilege(pgu.usename, scl.schema_name, 'USAGE')) as schema_name
from pg_user as pgu;
table 확인
-- 1.
select * from information_schema.role_table_grants
where table_schema not in ('pg_catalog', 'information_schema') and grantee = 'user';
-- 2.
select * from information_schema.table_privileges
where table_schema not in ('pg_catalog', 'information_schema');
-- 아래 SQL문을 실행하기 전에 search path를 잘 설정해주어야 한다.
set search_path to information_schema, [schema1], [schema2], [schema3] ;
-- 본 SQL문
-- 권한 파트를 수정해서 사용해야 한다. 'SELECT' 파트
select pgu.usename as user_name,
(select string_agg(ptb.tablename, ',' order by ptb.tablename)
from (select * from pg_tables
where schemaname not in ('pg_catalog', 'information_schema')) as ptb
where has_table_privilege(pgu.usename, ptb.tablename, 'SELECT')) as table_name
from pg_user as pgu
Check privilege - object 중심
- object 중심으로 권한 상황을 요약한 TABLE을 만들어주는 SQL문들이다.