๐Ÿ˜ PostgreSQL! - 2

sh_awtylol4ยท2022๋…„ 6์›” 22์ผ
0
post-thumbnail

Schema ์— ๊ด€ํ•œ ๊ฐœ๋…์ •๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™์•„ ๊ตฌ๊ธ€๋ง ๋„์ค‘ ์ข‹์€ ๊ธ€์„ ์ฐพ์•˜๋‹ค. ์ด ๊ธ€์„ ์˜ค๋Š˜ TIL์— ์ฐธ๊ณ  ์ž๋ฃŒ๋กœ ์“ฐ๋ ค๊ณ  ํ•œ๋‹ค.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป PostgreSQL์˜ Schema์˜ ์˜๋ฏธ ๋ฐ ๊ถŒํ•œ๊ด€๋ฆฌ!

  • Table ์€ ํ–‰(rows)๊ณผ ์—ด(columns)๋กœ ๋ฐ์ดํ„ฐ๋กœ ์ €์žฅํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, Schema์— ํฌํ•จ ๋˜์–ด์žˆ๋‹ค.

  • Schema ๋Š” ๋…ผ๋ฆฌ์ ์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•ด๋†“์€ ๊ฒƒ์œผ๋กœ์„œ, ์—ฌ๋Ÿฌ๊ฐœ์˜ Table / procedure / Function / Index / View ๋“ฑ์„ ํฌํ•ฉํ•˜๊ณ  ์žˆ๋‹ค.

  • DATABASE ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๊ณณ์— ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ชจ์•„๋‘” ๊ฒƒ์œผ๋กœ์„œ, ์—ฌ๋Ÿฌ๊ฐœ์˜ Schema๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

PostgreSQL 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๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋˜๋ฉด ํฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค!

-- ๋จผ์ € 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}"

์ฐธ๊ณ  ์ž๋ฃŒ-1
์ฐธ๊ณ  ์ž๋ฃŒ-2

profile
ํฌ๊ธฐํ•˜๋Š” ๊ทธ ์ˆœ๊ฐ„์ด ๋ฐ”๋กœ ์‹œํ•ฉ์ข…๋ฃŒ์˜ˆ์š”...

0๊ฐœ์˜ ๋Œ“๊ธ€