LoginSignup
2
7

More than 3 years have passed since last update.

DjangoでWebアプリを作ってみる。【vol. 03 DBをPostgreSQLに変更する。】

Last updated at Posted at 2019-06-23

こちらの記事の続きです。

DBをPostgreSQLに変更する。

こちらの記事を参考にさせていただきました。

元々Sqliteが入っているのですが、普段使っているのがPostgreSQLなので変更したいと思います。
※すでにPostgreSQLのインストールは終わっている前提です。

psycopgのインストール

こちらが管理サイトになっています。
現在の最新は、

The current stable Psycopg release is 2.8.3:

となっています。

コマンドラインからインストール可能だったので、pipを使って実行しました。

pip install psycopg2
Collecting psycopg2
  Downloading https://files.pythonhosted.org/packages/3b/b4/b6db75663e1c73bb6190cbcbb02f94a36c574b813a353446087cbdf43712/psycopg2-2.8.3-cp37-cp37m-win_amd64.whl (1.1MB)
     |████████████████████████████████| 1.1MB 501kB/s
Installing collected packages: psycopg2
Successfully installed psycopg2-2.8.3

DB接続定義の修正

「settings.py」内に76行目あたりにDB接続定義の記載があったので以下修正しました。

DATABASES = {
    # 'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    # }
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'POST': '5432'
    }
}

マイグレーションの実行

マイグレーションを実行します。

python 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 sessions.0001_initial... OK

pgAdmin4で確認してみたところ、

pgadmin.JPG

確かにDjango関連のテーブルが作成されています。

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7