본문 바로가기
WEB/Django

Django 파이썬 웹 프로그래밍(4) Postgresql 설치 및 연결

by Guardy 2020. 7. 26.
728x90

Database를 담당하는 Postgresql 설치 와 연결을 하도록 하겠다.

https://www.postgresql.org/download/windows/

 

PostgreSQL: Windows installers

Windows installers Interactive installer by EDB Download the installer certified by EDB for all supported PostgreSQL versions. This installer includes the PostgreSQL server, pgAdmin; a graphical tool for managing and developing your databases, and StackBui

www.postgresql.org

에서  12.3 버전을 받도록한다. 12.3버전을 받는 이유는 Cookiecutter에서 12.3을 선택했기 때문이다.

cookiecutter에서 다른 버전을 선택했다면 해당하는 버전을 다운로드 받으면 된다.

다운을 받고 설치를 하도록 하자.

설치는 기본 설정대로 설치를 진행하면된다. password의 경우 local 환경에서 사용할 pasword이기 때문에 필자는 root1234로 하도록 하겠다. 후에 웹에 올라갈때는 복잡한 password로 바꿔줘야한다.

설치가 다 되었다면 pgAdmin4를 열어주도록 하자.

pgAdmin을 깔면 다음과 같은 화면이 나온다. password는 root1234로 선택했기 때문에 이 것으로 로그인한다.

community 데이터베이스를 생성해준다.

 

그다음 base.py의 DATABASES 부분을 다음과 같이 수정한다.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'community',
        'USER': 'postgres',
        'PASSWORD': 'root1234',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

 

DATABASE를 연결했으니, 저번 글에서 models.py에 정의해준 것들을 migration 작업을 통해 community database에 넘겨주어야 한다. 다음과 같이 콘솔창에 입력한다.

python manage.py makemigrations
python manage.py migrate

이번 글에서는 postgresql 설치와 django 연결까지 하였다.

또한 migration을 통해 DB에 SQL문을 반자동으로 넘겨주었다.

다음 글에서는 admin page에서 Database 확인을 하도록 하겠다.

728x90