ย
1. PostgreSQL ์ด๋?1. PostgreSQL ์ค์น2. PostgreSQL ์ฌ์ฉํ๊ธฐ1. PostrgeSQL ์คํ2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ3. ์ ์ ์์ฑ ๋ฐ ์ค์ 4. ํด๋ผ์ด์ธํธ ์ธ์ฆ ํ์ผ ์์ 3. ์ฅ๊ณ ์ PostgreSQL ์ฐ๊ฒฐ
ย
์ด๋ฒ์๋ PostgreSQL์ ํฐ๋ฏธ๋์์ ์ธํ
ํด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ, ์ ์ ๊ถํ ๋ฐ ์์ ๊ถ ๋ณ๊ฒฝ, Django์ ์ฐ๊ฒฐ์ ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ์๋ ๋ด์ฉ์ ๋ฐ๋ผ ํ์๋ค๊ฐ ์ด๋ ค์ฐ์๋ฉด PostgreSQL ์ ์ฉ์ ๋์ค์ ํ์๋ ๊ฒ์ ์ถ์ฒ๋๋ฆฝ๋๋ค. ์ฐ์ Django์ ๊ตฌ์กฐ๊ฐ ์ต์ํด์ง์ ํ์ ์ ์ฉํ์
๋ ์๋ฌด ๋ฌธ์ ์์ต๋๋ค.
ย
1. PostgreSQL ์ด๋?
PostgreSQL์ ๊ต์ฅํ ๊ฐ๊ด๋ฐ๊ณ ์๋ DB ์ค ํ๋์
๋๋ค.


ย
ย
๋ฌธ์ ๋ PostgreSQL์ ์ฌ์ฉํ๋ ค๋ฉด ํฐ๋ฏธ๋์์ ์์
์ ๋ง์ด ํด์ผํ๋ค๋ ์ ์
๋๋ค. ์์์ Django ์ธํ
ํ๋ ๋ถ๋ถ์ ์งํํ๋๋ฐ ํฐ๋ฏธ๋ ์ธํ
ํ๋ ๋ถ๋ถ์ด ๊ต์ฅํ ์ด๋ ค์ ๋ ๋ถ์ ์ด ๋ถ๋ถ์ ํจ์คํ์
๋ ์๊ด์์ต๋๋ค. ํจ์คํ์๋ ๋ถ๋ค์ ๋์ค์ ์ํ์ค ๋ ๋ฐฐ์ฐ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
ย
1. PostgreSQL ์ค์น
์ฐ์ , ํฐ๋ฏธ๋๋ก ์คํ์ ํ๊ฒ ์ต๋๋ค.
ย

ย
apt-get์ ์
๋ฐ์ดํธํด์ค๋๋ค.
root@goorm:/workspace/instaclone# apt-get update
ย

ย
apt-get์ ์ด์ฉํด postgreSQL์ ์ค์นํด์ค๋๋ค.
root@goorm:/workspace/instaclone# apt-get install postgresql Y
ย

ย

ย
2. PostgreSQL ์ฌ์ฉํ๊ธฐ
1. PostrgeSQL ์คํ
PostgreSQL ์คํ์ํค๊ฒ ์ต๋๋ค.
root@goorm:/workspace/instaclone# service postgresql start
ย

ย
PostgreSQL์ด ์๋ํ๋์ง ํ์ธํ๊ฒ ์ต๋๋ค.
root@goorm:/workspace/instaclone# ps -ef|grep postgres
ย

ย
PostgreSQL์ ์ค์น๊ฐ ๋๋ฉด์, ์ํผ ์ ์ ๊ฐ ์์ฑ๋ฉ๋๋ค. ์์ฑ๋ ์ํผ์ ์ ๋ก ์ ์์ ํด๋ณด๊ฒ ์ต๋๋ค.
root@goorm:/workspace/instaclone# su - postgres
ย

ย
psql์ด๋ผ ์
๋ ฅํ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ผ๋ก ๋ค์ด๊ฐ๋๋ก ํ๊ฒ ์ต๋๋ค.

ย
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ํ ๋ฒ ์์ฑํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. instaclone์ด๋ผ๋ ์ด๋ฆ์ database๋ฅผ ๋ง๋๋ ๋ช
๋ น์
๋๋ค. ;(์ธ๋ฏธ์ฝ๋ก )์ ๋น ํธ๋ฆฌ๋ฉด ์ ๋ฉ๋๋ค.
create database instaclone;
ย

ย
CREATE DATABASE ๋ผ๊ณ ์ถ๋ ฅ๋๋ฉด ์ฑ๊ณต์ ์ผ๋ก database๊ฐ ๋ง๋ค์ด ์ง ๊ฒ์
๋๋ค.
๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์ ์์ฑ ๋์๋์ง ํ์ธํด๋ณด๊ฒ ์ต๋๋ค.
\l
ย
instaclone database๊ฐ ๋ฆฌ์คํธ์ ์๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.

ย
3. ์ ์ ์์ฑ ๋ฐ ์ค์
์์ผ๋ก ๊ณ์ ์ํผ์ ์ ๋ฅผ ์ฌ์ฉํ ์๋ ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๊ทผ ๊ถํ์ ๊ฐ์ง ์ ์ ๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
ย
๋จผ์ ์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ๊ฐ์ง ์ ์ ๋ฅผ ์์ฑํฉ๋๋ค.
create user username with password '12341234';
์ ์ ์ด๋ฆ์ username์ด๊ณ ๋น๋ฐ๋ฒํธ๋ '12341234' ์ธ ์ ์ ๋ฅผ ๋ง๋ค๊ฒ ๋ค๋ ๋ช
๋ น์ด์
๋๋ค.
username๊ณผ password๋ ๋ณธ์ธ์ด ์ ํฉ๋๋ค.
ย
CREATE ROLE ์ด ์ถ๋ ฅ๋๋ฉด์ ๋ช
๋ น์ด ์ํ๋์๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค.

ย
๋ช ๊ฐ์ง ์ค์ ์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
- ์ ์ ์ ์ธ์ฝ๋ฉ์ utf-8๋ก ๋ฐ๊ฟ๋๋ค.
alter role username set client_encoding to 'utf-8';
ย

ย
- ์ ์ ์ ์๊ฐ์ ์์ธ ๊ธฐ์ค์ผ๋ก ๋ฐ๊ฟ๋๋ค.
alter role username set timezone to 'Asia/Seoul';
ย

ย
- instaclone ์ด๋ผ๋ database์ ๊ถํ์ username์๊ฒ ๋ถ์ฌํฉ๋๋ค.
grant all privileges on database instaclone to username;
ย

ย
\l ๋ช
๋ น์ด๋ฅผ ํตํด ๋ฐ๋ ์ค์ ์ ํ์ธํด๋ด
๋๋ค.

ย
Access privileges ์ ๊ทผ ๊ถํ์ username์ด ์ถ๊ฐ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.

ย
instaclone ์ ์ ๋ก์ ์ ๊ทผ ๊ถํ์ ๊ฐ์ง๊ฒ ๋์์ง๋ง, Owner๊ฐ postgres๋ก ๋์ด ์๊ธฐ ๋๋ฌธ์ username์ผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์์ ํ ๋ ๋ก๊ทธ์ธ์ด ์ ๋ฉ๋๋ค. ๊ทธ๋์ username๋ฅผ ์ค๋๋ก ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
ย
instaclone ์ด๋ผ๋ DATABASE์ OWNER๋ฅผ username์ผ๋ก ๋ณ๊ฒฝํ๋ค๋ ๋ช
๋ น์ด์
๋๋ค.
ALTER DATABASE instaclone OWNER TO username;
ย

ย
์ด์ instagram ๋ฐ์ดํฐ๋ฒ ์ด์ค์ Owner๊ฐ username์ผ๋ก ๋ณ๊ฒฝ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.

ย
4. ํด๋ผ์ด์ธํธ ์ธ์ฆ ํ์ผ ์์
ํด๋ผ์ด์ธํธ ์ธ์ฆ์ ๊ดํ ํ์ผ์ ์์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค. ์์ ํ ํ์ผ์
pg_hba.conf
๋ผ๋ ํ์ผ์
๋๋ค. ย
๋จผ์ , ํ์ผ์ด ์ด๋ ์๋์ง ํ์ธํด๋ณด๊ฒ ์ต๋๋ค.
show hba_file;
ย
pg_hba.conf
์ ๊ฒฝ๋ก๊ฐ ๋์ต๋๋ค.
ย
ํด๋น ๊ฒฝ๋ก์ ํ์ผ์ vim ์๋ํฐ๋ฅผ ์ฌ์ฉํด์ ํ์ผ ๋ด์ฉ์ ์์ ํ ๊ฒ์
๋๋ค.
\q ๋ช
๋ น์ด๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋น ์ ธ๋์ค๊ฒ ์ต๋๋ค.

ย
์ด์ ๊ฐ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋น ์ ธ๋์จ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ์ฌ๊ธฐ์ exit ๋ช
๋ น์ด๋ฅผ ์น๋ฉด postgreSQL์ ์์ ํ ์ข
๋ฃํ ์ ์์ต๋๋ค.

ย
์๊น ํด๋น ๊ฒฝ๋ก์ ํ์ผ์ vim ์๋ํฐ๋ก ์ฝ๋๋ค. ์๊น ๊ฒฝ๋ก๋ฅผ ํ์ธํ๋ ๊ฒ์ ํ์ฉํฉ๋๋ค. ๊ฒฝ๋ก๋ ๋ฒ์ ๋ง๋ค ๋ค๋ฅผ ์ ์์ผ๋ ์ฐธ๊ณ ํด ์ฃผ์๊ธธ ๋ฐ๋๋๋ค.
root@goorm:/workspace/instaclone/instaclone# vim /etc/postgresql/10/main/pg_hba.conf
ย

ย
๋ฐฉํฅํค๋ก ์์ ํ ์์น๋ฅผ ์ฐพ์๊ฐ๋ ค๊ณ ํฉ๋๋ค. ๊ทธ ์ ์ ์ค ๋ฒํธ๋ฅผ ํ์ํ๋ ค๊ณ ํฉ๋๋ค. ์ผ๋จ :(์ฝ๋ก )์ ์
๋ ฅํฉ๋๋ค.

ย
:(์ฝ๋ก )์ ์
๋ ฅํ๋ฉด ํ๋ฉด ํ๋จ์ : ์ด ์๊น๋๋ค. Vim์ ๋ช
๋ น์ด๋ฅผ ์
๋ ฅํ ์ ์๋ ์ํ๊ฐ ๋ ๊ฒ์
๋๋ค. ์ฌ๊ธฐ์ set number ๋๋ ์ค์ฌ์ set nu์ ์
๋ ฅํ๊ณ Enter๋ฅผ ์ณ๋ด
๋๋ค.
:set number :set nu
ย

ย
๊ทธ๋ฌ๋ฉด ์ค ๋ฒํธ๊ฐ ํ์๋ฉ๋๋ค. ์ด์ ๋ฐฉํฅํค๋ก ํ์ผ ๋ด์ฉ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ๋ฐฉํฅํค๋ก ์ปค์ ์์น๋ฅผ ์กฐ์ ํ ์ ์์ต๋๋ค. ์๋ ๋ถ๋ถ์ Unix domain socket connections only ๋ผ๋ ๋ถ๋ถ์ด ์์ต๋๋ค.

ย
์ฌ๊ธฐ์ METHOD๊ฐ peer๋ก ๋์ด์์ต๋๋ค. ์ด๊ฒ์ ์๋ ํญ๋ชฉ๊ณผ ๊ฐ์ด md5๋ก ๋ฐ๊ฟ ๊ฒ์
๋๋ค.

ย
vim ์์ ์์ ๋ชจ๋๋ก ๋ค์ด๊ฐ๊ธฐ ์ํด์๋ i(insert) ๋ฅผ ๋๋ฆ
๋๋ค. (ํ์ํค ํ์ธํด์ฃผ์
์ผ ๋ฉ๋๋ค.)
i ๋ฅผ ๋๋ฅด๋ฉด ํ๋ฉด ํ๋จ์ โ ๋ผ์๋ฃ๊ธฐ โ ๋ผ๊ณ ์ถ๋ ฅ๋ฉ๋๋ค.

ย
์ปค์๋ฅผ peer ๋ก ์ฎ๊ฒจ์ Delete ํค๋ก ์ง์ฐ๊ณ m๋ฅผ ์
๋ ฅํฉ๋๋ค.

ย
esc ๋ฅผ ๋๋ฌ์ ๋ผ์ ๋ฃ๊ธฐ ๋ชจ๋์์ ๋น ์ ธ๋์ต๋๋ค.
ย
๋ง์ง๋ง์ผ๋ก ํ์ผ์ ์ ์ฅํด์ผ ํฉ๋๋ค. :(์ฝ๋ก )์ ๋๋ฅด๊ณ wq ๋ช
๋ น์ด๋ฅผ ์
๋ ฅํฉ๋๋ค.
:wq
wq๋ ์ ์ฅ(write)ํ๊ณ ๋๊ฐ๋ค(quit)๋ ์๋ฏธ์
๋๋ค. ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ์์ ํ๊ณ ๋์์ต๋๋ค.
ย

ย
๋ณ๊ฒฝ ์ฌํญ์ ์ ์ ์ฉ์ํค๊ธฐ ์ํด์ ์๋ฒ๋ฅผ ์ฌ์์ ํ๊ฒ ์ต๋๋ค.
root@goorm:/workspace/instaclone/instaclone# service postgresql restart
ย
์
๋ ฅํ๋ฉด, done์ด ์ถ๋ ฅ๋๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.

ย
username ์ ์ ๋ก ์ ์ํด๋ณด๊ฒ ์ต๋๋ค. ์ผ๋จ postgres๋ฅผ ์คํํฉ๋๋ค.
root@goorm:/workspace/instaclone/instaclone# su - postgres
ย

ย
postgres๊ฐ ์คํ๋๋ฉด ๋ค์ ๋ช
๋ น์ด๋ฅผ ํตํด username์ผ๋ก instaclone ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์์ ์๋ํฉ๋๋ค.
psql -U username -d instaclone
ย
๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ๋ผ๊ณ ๋์ต๋๋ค. ๋น๋ฐ๋ฒํธ๋ ์๊น user๋ฅผ ์์ฑํ ๋ ์ค์ ํ ๊ฒ์ ๋งํฉ๋๋ค.

ย
๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ๋ฉด, ์ฑ๊ณต์ ์ผ๋ก ์ ์ ๋ก instaclone ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ํ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
ํ์ฌ username ์ ์ ๋ก ์ ์ํ ๊ฒ์
๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์์ ํด์ผ ๋๋ ์ธํ
์ ์ ๋ถ ๋๋ฌ์ต๋๋ค.
ย
3. ์ฅ๊ณ ์ PostgreSQL ์ฐ๊ฒฐ
Django ์์ ์๋
settings.py
๋ฅผ ์์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค. ๊ตฌ๋ฆ IDE ์ปจํ
์ด๋๋ก ๋์๊ฐ๊ฒ ์ต๋๋ค.๋จผ์ , ํฐ๋ฏธ๋์์ ํ๋ก์ ํธ ํด๋๋ก ๋๋ ํ ๋ฆฌ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
root@goorm:/workspace/instaclone/instaclone# cd instaclone
ย
์ด๋, (master) ๋ผ๋ ํ์๋ Git์ด ์ถ์ ํ๋ ํด๋๋ผ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.

ย
๊ฐ์ ํ๊ฒฝ(venv)์ ํ์ฑํ ํฉ๋๋ค. ์ฅ๊ณ ํ๋ก์ ํธ ๊ด๋ จ ์์
์ ํ๊ธฐ ์ ์๋ ํญ์ ๊ฐ์ ํ๊ฒฝ์ด ํ์ฑํ ๋์ด์๋์ง ํ์ธํ๋ ์ต๊ด์ด ํ์ํฉ๋๋ค.
root@goorm:/workspace/instaclone/instaclone# source venv/bin/activate
ย

ย
config/settings.py
ํ์ผ์ ์ด๊ฒ ์ต๋๋ค.

ย
config/settings.py
76๋ฒ์งธ ์ค์ DATABASES
๋ณ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
ย
์ด ๋ถ๋ถ์ Django์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ด๋ จ๋ ๋ด์ฉ์ ๋์
๋๋ฆฌ ํํ๋ก ๋ด๊ณ ์์ต๋๋ค. ํ๋ก์ ํธ ์์ฑ์ default ๊ฐ์ผ๋ก SQLite3๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ์ฌ์ฉํ๋ค๊ณ ์ ํ์์ต๋๋ค. ์ด๊ฒ์ ์์ ํ์ง ์์ผ๋ฉด PostgreSQL์ ์ค์นํด๋ Django๋ SQLite3๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ์ฌ์ฉํ๊ฒ ๋๋ ๊ฒ์
๋๋ค.
ย
DATABASES์ 'default' ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํฉ๋๋ค.
ย
ํ์ผ๋ช
:
config/settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'instaclone', 'USER': 'username', 'PASSWORD': '12341234', 'HOST': 'localhost', 'PORT': '', } }
ย
์์ผ๋ก๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ด๋ จ๋ ์ ๋ณด๋ฅผ ์ด ๋ถ๋ถ์์ ๋ค๋ฃฐ ์ ์์ต๋๋ค. Django๋ ๋ค์ํ DB๋ฅผ ์ฌ์ฉํ๊ธฐ ์ข์ ํ๊ฒฝ์ ๊ฐ๊ณ ์์ต๋๋ค. ์ถํ์ ๋ค๋ฅธ DB๋ ์ฌ์ฉํด๋ณผ ๋๋ ์ด ๋ถ๋ถ์ ์์ ํ๋ฉด ๋๊ฒ ์ต๋๋ค.
์ด์
migrate
ํ๋ฉด Django๊ฐ PostgreSQL์ ๋ง๊ฒ ๋ด์ฉ์ ๋ณ๊ฒฝํด์ค๋๋ค. (venv)root@goorm:/workspace/instaclone/instaclone# python manage.py migrate
ย
ํ์ง๋ง ์๋์ ๊ฐ์ด psycopg2๋ผ๋ ๋ชจ๋์ด ์๋ค๋ ์๋ฌ๊ฐ ํ์๋ ์ ์์ต๋๋ค.
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
ย
์ด ๊ฒฝ์ฐ pip๋ฅผ ์ด์ฉํด psycopg2-binary๋ฅผ ์ค์นํฉ๋๋ค.
(venv)root@goorm:/workspace/instaclone/instaclone# pip install psycopg2-binary
ย

ย
psycopg2-binary๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ค์นํ ๋ค, ๋ค์ migrate๋ฅผ ์๋ํฉ๋๋ค.
(venv)root@goorm:/workspace/instaclone/instaclone# python manage.py migrate
ย

ย
No module named 'imagekit' ๋ผ๊ณ ์ถ๋ ฅ๋ ๋, django-imagekit๋ฅผ ์ค์นํฉ๋๋ค.
root@goorm:/workspace/instaclone/instaclone# pip install django-imagekit
ย
No module named 'PIL' ๋ผ๊ณ ์ถ๋ ฅ๋ ๋, pillow๋ฅผ ์ค์นํฉ๋๋ค.
root@goorm:/workspace/instaclone/instaclone# pip install pillow
ย