1 ๋ถ„ ์†Œ์š”

๋จผ์ € DB๋ฅผ ์ƒ์„ฑํ•˜์ž!

> psql postgres
postgres=# create database [database_name];

๊ทธ ๋‹ค์Œ DB์— ์ ‘์†ํ•ด์•ผ๊ฒ ์ง€

postgres=# \connect[database_name];

๊ทธ ๋‹ค์Œ์—” ์ƒˆ๋กœ์šด ์œ ์ €๋ฅผ ๋งŒ๋“ค๊ณ , ๋ช‡ ๊ฐ€์ง€ ์„ค์ •์„ ํ•ด์•ผํ•จ

=# create user root with password 'password';
  • root: ์œ ์ € ์ด๋ฆ„
  • password: ๋น„๋ฐ€๋ฒˆํ˜ธ โ†’ ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ
alter role root set client_encoding to 'utf-8'

alter role root set timezone to 'Asia/Seoul';

grant all privileges on database [database_name] to root;

์—ฌ๊ธฐ๊นŒ์ง€๋Š” PostgreSQL ์„ค์ •์ด์—ˆ๊ณ !


์ด์ œ Django ์„ธํŒ… ์‹œ์ž‘!

psycopg2 ์„ค์น˜

> pip install psycopg2

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'project',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

๋งŒ์•ฝ! AssertionError: database connection isnโ€™t set to UTC ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋œจ๋ฉด django์™€ psycopg2์˜ ๋ฒ„์ „ ํ˜ธํ™˜ ๋ฌธ์ œ์ž„. psycopg2๋ฅผ ๋‹ค์šด๊ทธ๋ ˆ์ด๋“œ ํ•˜๊ฑฐ๋‚˜, settings.py์—์„œ USE_TZ = True ๋ฅผ ์‚ญ์ œํ•˜๋ฉด ๋œ๋‹ค.

DB ํ…Œ์ด๋ธ” ์ •๋ณด๋ฅผ models.py๋กœ ๋ฐ›์•„์˜ค์ž!

DB table์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ง„ํ–‰!

> python manage.py inspectdb > models.py
  • ํ…Œ์ด๋ธ” ์ •๋ณด๊ฐ€ models.py์— ์˜ฎ๊ฒจ ๋‹ด์•„์ง„๋‹ค!

# Unable to inspect table โ€˜table_nameโ€™ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด GRANT SELECT ON TABLE [table_name] TO [user_name]; ์œผ๋กœ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ ํ›„ ๋‹ค์‹œ ํ•ด๋ณด์ž.

  • ๋ชจ๋“  ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๊ถŒํ•œ ๋ถ€์—ฌํ•˜๊ธฐ - ์ฃผ์˜ํ•˜์—ฌ ์‚ฌ์šฉ(๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ)
```
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO [์‚ฌ์šฉ์ž๋ช…];
```

models.py์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ๊ฒผ์œผ๋‹ˆ, ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•ด์•ผ๊ฒ ์ง€?

> python manage.py startapp [app_name]
INSTALLED_APP = [
	...
	'app_name', # ์•ฑ ์ถ”๊ฐ€
	...
]

์—ฌ๊ธฐ์„œ, ๊ธฐ์กด models.py๋ฅผ ์•ฑ ์•ˆ์˜ models.py์— ๋ฎ์–ด์”Œ์›Œ์ฃผ์ž.

> python manage.py makemigrations
> python manage.py migrtate

์—ฌ๊ธฐ์„œ django_migrations table (permission denied for schema public LINE 1: CREATE TABLE โ€œdjango_migrationsโ€ (โ€œidโ€ bigint NOT NULL PRIMAโ€ฆ ์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, psql์—์„œ alter database [database_name] owner to [user_name]; ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ ํ›„์— ๋‹ค์‹œ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. โ†’ ํ”„๋กœ์ ํŠธ DB์˜ ์†Œ์œ ์ž ๋กœ ๊ถŒํ•œ์„ ์ฃผ๋Š”๊ฑฐ๋ผ๊ณ  ํ•จ.

์ฐธ๊ณ  ์ž๋ฃŒ

CREATE TABLE โ€œdjango_migrationsโ€ (โ€œidโ€ bigint NOT NULL PRIMA

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ