PostGresql 안정화 버젼 설치 및 django과 연결

우분투 버젼확인

#input
cat /etc/issue

#output
Ubuntu 18.04.5 LTS

설치 진행 (안정화 버젼)

# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql

버젼 확인

#input
psql -V

#output
psql (PostgreSQL) 13.4 (Ubuntu 13.4-1.pgdg18.04+1)

외부 접속전용 계정 생성

해당 계정은 추후 외부 접속/django연결을 위한 목적의 계정으로 생성됩니다.

postgreql은 postgres유저로 터미널 환경을 이용하여 명령어 활용이 가능합니다.

sudo su postgres

psql

#postgreql 터미널
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

create user 유저이름 superuser;
alter user euangadmin with password '암호';

#postgreql의 외부접속을 설정하는 파일이며, 이에 대한 경로를 확인할 수 있는 명령어 입니다.
postgres=# show hba_file;
              hba_file               
-------------------------------------
 /etc/postgresql/13/main/pg_hba.conf

pg_hba.conf수정

로컬이 아닌 외부에서 수정작업이 가능하도록 아래 내용을 추가합니다.

# postgresql의 외부 접속을 허용
host    all             all             0.0.0.0/0               md5

postgresql을 재시작 합니다.

sudo systemctl restart postgresql

django 프로젝트와 연결

settings.py에 해당 프로젝트를 연결합니다.

...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': '아이디',
        'PASSWORD': '비밀번호',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

패키지 명령어를 통해 해당 모듈을 설치해 줍니다. psql과 django를 연결해주는 역할을 합니다.

pip install psycopg2-binary

runserver로 올려보기

migrate작업은 현재 django에 default로 만들어진 파일과, 어플리케이션 레벨에서 직접 만든 정보를 참조하여 테이블을 만들어 주는 작업을 말합니다.

기존에 migrate작업을 진행한 것이 연결되어진 db단에서 확인이 안되어지는 경우, django서버에서는 migrate가 안된 것에 대해서 경고를 합니다.

migrate를 진행합니다.

python3 manage.py migrate

아래처럼 성공적으로 migrate를 진행하게 되면 반영이 끝났음을 알 수 있습니다.

(venv) ubuntu@ip-XXXXXXXXX:~/django_projects/euang$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

좋은 웹페이지 즐겨찾기