LoginSignup
8
3

More than 5 years have passed since last update.

Python + Django + psycopg2でposgreSQLに接続する

Posted at

1.psycopg2をインストール

postgreSQLのドライバをインストールします

cmd.prompt
c:\python\env1>Scripts\activate
(env1) c:\python\env1>cd ..
(env1) c:\python>cd mysite
(env1) c:\python\mysite>pip install psycopg2
(env1) c:\python\mysite>python -c "import psycopg2"

仮想環境は、C:\python\env1に作成しています。

2.データベースを作成する

create database djangodb

3.setting.pyを編集

setting.py
DATABASES = {
           'default': {
           'ENGINE': 'django.db.backends.postgresql_psycopg2',
           'NAME': 'djangodb',
           'USER': 'testuser',
           'PASSWORD': 'xxxxxxxxxxxxxxxx',
           'HOST': '127.0.0.1',
           'POST': '5432'
         }
   }

3.migrate

(env1) c:\python>myproject1\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 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 sessions.0001_initial... OK

サーバを起動して確認する

(env1) c:\python>myproject1\manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
April 30, 2018 - 01:07:17
Django version 2.0.4, using settings 'myproject1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

大丈夫そうです。

8
3
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
8
3