๐Ÿ˜ Django์—์„œ PostgreSql ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •

6788 ๋‹จ์–ด djangodatabasepythonpostgres

๋น ๋ฅธ ์กฐ์–ธ:



Django ์•ฑ์˜ ๋ฐฐํฌ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒŒ์‹œ๋ฌผ์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๊ฒŒ์‹œ๋ฌผ๋กœ ์ด๋™ํ•˜์‹ญ์‹œ์˜ค.




heroku์—์„œ ์•ฑ ๋งŒ๋“ค๊ธฐ



heroku์—์„œ ๋ฌด๋ฃŒ ์•ฑ์„ ๋งŒ๋“ค๊ธฐ ์ „์— ๊ณ„์ •here์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ณ„์ •์„ ์ƒ์„ฑํ•˜๋ฉด ์ด์™€ ๊ฐ™์€ ๋Œ€์‹œ๋ณด๋“œ๊ฐ€ โ€‹โ€‹ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.



์•ฑ์„ ๋งŒ๋“ค๋ ค๋ฉด New๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ƒˆ ์•ฑ ๋งŒ๋“ค๊ธฐ ์˜ต์…˜์ด ์žˆ๋Š” ๋“œ๋กญ๋‹ค์šด์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๊ฒƒ์„ ์„ ํƒํ•˜๊ณ  ์ด๋ฆ„์„ ์„ ํƒํ•œ ๋‹ค์Œ ์•ฑ ๋งŒ๋“ค๊ธฐ๋ฅผ ํด๋ฆญํ•˜์‹ญ์‹œ์˜ค.


์ด์ œ ์ƒˆ ์•ฑ์„ ๋งŒ๋“ค์—ˆ์œผ๋ฏ€๋กœ "๊ฐœ์š”"๋ฅผ ํด๋ฆญํ•˜๊ณ  ์• ๋“œ์˜จ ๊ตฌ์„ฑ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ heroku postgres๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

์„ค์ • ํŒŒ์ผ ์ˆ˜์ •



์ด์ œ heroku์— ๋ฐฐํฌํ•  ์ค€๋น„๊ฐ€ ๊ฑฐ์˜ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฐฐํฌํ•  ์ค€๋น„๊ฐ€ ๋˜๋„๋ก ์„ค์ •์„ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

django_heroku ๋ฐ python decouple ์„ค์น˜

pip install gunicorn django-heroku python-decouple


๋‹ค๋ฅธ ์ง€์นจ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •์— ๋Œ€ํ•ด์„œ๋งŒ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

dj_database_url ์‚ฌ์šฉ


dj_database_url๋Š” django_heroku์™€ ํ•จ๊ป˜ ๋ฒˆ๋“ค๋กœ ์ œ๊ณต๋˜๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค.

ํ•จ์ˆ˜dj_database_url.config()๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ URL ๊ตฌ์„ฑ์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ˆ˜์‹ ํ•˜๊ณ  ์ด๋ฅผ ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํ‚ค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ˆ˜์ •:

# Import this packages previously dowloaded

import django_heroku
import dj_database_url
from decouple import config

# Go down until you find database stuff

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases


# Commment out the sqlite database setup

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }

# These lines make all the magic
DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}




ํ”„๋กœํ•„ ๋งŒ๋“ค๊ธฐ

์•ฑ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ์ด๊ฒƒ์„ ์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค.




touch Procfile


ํŒŒ์ผ์„ ์—ด๊ณ  ๋‹ค์Œ ์ค„์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

web: gunicorn (Your app).wsgi


์ด๊ฒƒ์€ Heroku์—๊ฒŒ gunicorn์„ HTTP ์„œ๋ฒ„๋กœ ์‚ฌ์šฉํ•˜๊ณ  (Your app)์— ์žˆ๋Š” wsgi ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ์›น ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•จ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. (๊ท€ํ•˜์˜ ์•ฑ)์„ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋™๊ฒฐ์š”๊ตฌ์‚ฌํ•ญ.txt



์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ์š”๊ตฌ ์‚ฌํ•ญ์œผ๋กœ ํŒŒ์ผ์„ ๊ณ ์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค.

pip freeze > requirements.txt


Heroku๋Š” ํ•ด๋‹น ํŒŒ์ผ์„ ๊ฒ€์ƒ‰ํ•˜๋ฏ€๋กœ ํ•ด๋‹น ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค.

Github๋กœ ๋ฐฐํฌ ๊ด€๋ฆฌ



Github๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฐํฌ๋Š” Heroku CLI ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ์‰ฌ์šฐ๋ฏ€๋กœ ๋ฐฐํฌํ•˜๋ ค๋Š” ์•ฑ์œผ๋กœ Github์— ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค.

๋ณ€๊ฒฝ ์‚ฌํ•ญ ํ‘ธ์‹œ

git add -A
git commit -m "Database Settings ready"
git push origin


์ด์ œ heroku ์›น์‚ฌ์ดํŠธ์—์„œ Deploy ํƒญ์œผ๋กœ ์ด๋™ํ•˜์—ฌ Connect with github๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.



๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด Github๋กœ ๋ฆฌ๋””๋ ‰์…˜๋ฉ๋‹ˆ๋‹ค.

์ด์ œ ๋ฐฐํฌํ•˜๋ ค๋Š” ์ €์žฅ์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์‹ญ์‹œ์˜ค.



๋งˆ์ง€๋ง‰์œผ๋กœ ์ˆ˜๋™ ๋ฐฐํฌ์—์„œ Deploy branch๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.


์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๊ธฐ๋‹ค๋ฆฌ์‹ญ์‹œ์˜ค ....

Heroku docs์„ ์ฝ๊ฑฐ๋‚˜ ๋ฌธ์ œ ์•„๋ž˜์— ์˜๊ฒฌ์„ ๋งํ•˜์‹ญ์‹œ์˜ค.

์‹œ๊ฐ„ ๋‚ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์ด ์œ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค ๐Ÿค—.

Follow me in My blog,
to get more awesome tutorials like this one.
Please consider supporting me on Ko-fi you help me a lot to
continue building this tutorials!.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ