Schema ์ ๊ดํ ๊ฐ๋ ์ ๋ฆฌ๊ฐ ๋ถ์กฑํ ๊ฒ ๊ฐ์ ๊ตฌ๊ธ๋ง ๋์ค ์ข์ ๊ธ์ ์ฐพ์๋ค. ์ด ๊ธ์ ์ค๋ TIL์ ์ฐธ๊ณ ์๋ฃ๋ก ์ฐ๋ ค๊ณ ํ๋ค.
Table
์ ํ(rows)๊ณผ ์ด(columns)๋ก ๋ฐ์ดํฐ๋ก ์ ์ฅํ๊ณ ์์ผ๋ฉฐ, Schema์ ํฌํจ ๋์ด์๋ค.
Schema
๋ ๋
ผ๋ฆฌ์ ์ผ๋ก ํ
์ด๋ธ์ ์ ์ํด๋์ ๊ฒ์ผ๋ก์, ์ฌ๋ฌ๊ฐ์ Table / procedure / Function / Index / View ๋ฑ์ ํฌํฉํ๊ณ ์๋ค.
DATABASE
๋ ๋ฐ์ดํฐ๋ฅผ ํ ๊ณณ์ ๊ด๋ฆฌํ ์ ์๋๋ก ๋ชจ์๋ ๊ฒ์ผ๋ก์, ์ฌ๋ฌ๊ฐ์ Schema๋ฅผ ๊ฐ์ง ์ ์๋ค.
MySQL
์์๋ ๋
ผ๋ฆฌ DB๋ฅผ Schema ์ ๊ฐ์ ์๋ฏธ๋ก ์ฌ์ฉํ๋ค.
๋ฐ๋ฉด PostgreSQL
์์ DB์ Schema ๋ ๊ฐ์ง ๊ฐ๋
๋ชจ๋ ์ฌ์ฉ๋๋ฉฐ DB๋ Schema์ ์์ ๊ฐ๋
์ด๋ผ ํ ์ ์์ต๋๋ค.
Table์ ์งํฉ์ schema ๋ผ๊ณ ํํํ๋ฉฐ ์ด schema๋ ํ๋์ DB๋ฅผ ๋ ผ๋ฆฌ์ ์ผ๋ก ๋๋๋ ๊ฐ๋ ์ ๋๋ค.
์ฆ, MySQL
์์์ ๋
ผ๋ฆฌ DB๋ PostgreSQL
์์์ Schema๋ผ๊ณ ํ ์ ์์ต๋๋ค.
์ด๋ฐ ์ฐจ์ด์ ๋๋ฌธ์ PostgreSQL
์์๋ ํ๋์ DB instance์ ์๋คํด๋ ์๋ก ๋ค๋ฅธ DB์ ์๋ ํ
์ด๋ธ ๊ฐ์๋ ์๋ก JOIN ์ฐ์ฐ์ ํ ์ ์์ต๋๋ค.
๋์ ์๋ก ๋ค๋ฅธ Schema์ ํ ์ด๋ธ ๊ฐ์ JOIN ์ฐ์ฐ์ด ๊ฐ๋ฅํฉ๋๋ค.
๋ค๋ฅธ ์ ์ ๊ฐ DB๋ฅผ ์์ฑํ๊ณ ์ ๊ทผ ํ ์ ์๊ฒ๋๋ฉด ํฐ ๋ฌธ์ ๊ฐ ์๊ธด๋ค!
-- ๋จผ์ db๋ฅผ ์์ฑํฉ๋๋ค. (public schema๊ฐ ์์ฑ๋๊ฒ ์ฃ .)
postgres=# create database svcdb with owner svcuser;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
svcdb | svcuser | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
-- ๋ค๋ฅธ ์๋น์ค ์ ์ ์ ์์ฑํฉ๋๋ค.
postgres=# create user anotheruser with password 'secret';
CREATE ROLE
-- ๋ค๋ฅธ ์๋น์ค์ ์ ๋ก๋ ์ ๊ทผ์ด ์๋๊ณ ํ
์ด๋ธ๋ ์ ๋ง๋ค์ด์ง๋๋ค.
postgres=# \c svcdb anotheruser
You are now connected to database "svcdb" as user "anotheruser".
-- postgres ์ ์ ๋ก ๊ถํ ํ์
svcdb=> \c svcdb postgres
You are now connected to database "svcdb" as user "postgres".
--๊ทธ๋ฆฌ๊ณ ์์ ์๊ฐ ์๋ ์ ์ ๋ค์ DB ์ ๊ทผ ๊ถํ์ revoke ํฉ๋๋ค.
svcdb=# REVOKE ALL ON DATABASE svcdb FROM PUBLIC;
REVOKE
svcdb=# \c svcdb anotheruser
FATAL: permission denied for database "svcdb"
DETAIL: User does not have CONNECT privilege.
Previous connection kept
svcdb=# \c svcdb svcuser
You are now connected to database "svcdb" as user "svcuser".
๊ธฐ๋ณธ์ ์ผ๋ก ์ค์น๋ Postgres DB์์
-- postgres db ์ ๋ค๋ฅธ ์ ์ ์ ๊ทผ ์ฐจ๋จ ๋ฐ public schema ์ฌ์ฉ ๊ธ์ง
psql -d postgres -U postgres -c "REVOKE ALL ON SCHEMA public FROM PUBLIC"
psql -d postgres -U postgres -c "REVOKE ALL ON DATABASE postgres FROM PUBLIC"
์๋น์ค์์ ์ฌ์ฉํ ์ ์ ์์ฑ ๋ฐ DB ์์ฑํ ๊ถํ ์ ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ดํ๋ฉด ๋ฉ๋๋ค.
ํนํ DDL ๊ถํ์ ์ ๊ฑฐํ ์ดํ๋ฆฌ์ผ์ด์
์ฉ ์๋น์ค ๊ณ์ ๊ณผ
DDL ๊ถํ์ ๊ฐ์ง ์ดํ๋ฆฌ์ผ์ด์
์ฉ ์๋น์ค ๊ณ์ ์ ๋ถ๋ฆฌํ๊ณ ํ๋์ ์คํค๋ง๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํ๋ฉด ๋ฉ๋๋ค.
APPOWNER=newappo
APPUSER=newsvc
DBNAME=newdb
# psql -d postgres -U postgres -c "drop database ${DBNAME}"
# psql -d postgres -U postgres -c "drop user ${APPOWNER}"
# psql -d postgres -U postgres -c "drop user ${APPUSER}"
-- ์ ๊ท ์ ์ ์์ฑ
psql -d postgres -U postgres -c "create user ${APPOWNER} with password 'secret'"
psql -d postgres -U postgres -c "create user ${APPUSER} with password 'secret'"
-- ์ ๊ท DB ์์ฑ
psql -d postgres -U postgres -c "create database ${DBNAME} with OWNER=${APPOWNER}"
-- db ์ ๊ทผ ๊ถํ ๋ฐ public schema ์ ๊ทผ ๊ถํ revoke
psql -d ${DBNAME} -U ${APPOWNER} -c "revoke all on database ${DBNAME} from public"
psql -d ${DBNAME} -U postgres -c "revoke all on schema public from public"
-- ์ ๊ทdb์ schema ์์ฑ
psql -d ${DBNAME} -U ${APPOWNER} -c "create schema ${APPOWNER} authorization ${APPOWNER}"
-- ์๋น์ค์ฉ ์ ์ ์ ์ต์ํ์ ๊ถํ๋ง ๋ถ์ฌ
psql -d ${DBNAME} -U ${APPOWNER} -c "grant connect,TEMPORARY on database ${DBNAME} to ${APPUSER}"
psql -d ${DBNAME} -U ${APPOWNER} -c "grant usage on schema ${APPOWNER} to ${APPUSER}"
psql -d ${DBNAME} -U ${APPUSER} -c "alter role ${APPUSER} set search_path to ${APPOWNER}"
psql -d ${DBNAME} -U ${APPOWNER} -c "grant select, insert, update, delete on all tables in schema ${APPOWNER} to ${APPUSER}"
psql -d ${DBNAME} -U ${APPOWNER} -c "alter default privileges in schema ${APPOWNER} grant select, insert, update, delete on tables to ${APPUSER}"
psql -d ${DBNAME} -U ${APPOWNER} -c "grant usage on all sequences in schema ${APPOWNER} to ${APPUSER}"
psql -d ${DBNAME} -U ${APPOWNER} -c "alter default privileges in schema ${APPOWNER} grant usage on sequences to ${APPUSER}"