djangoでpostgresqlを使用
先ずは、postgresql側でdatabaseを作成
$ createdb sample
setting.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'sample', # Or path to database file if using sqlite3. 'USER': 'user', # Not used with sqlite3. 'PASSWORD': '****', # Not used with sqlite3. 'HOST': '192.168.0.1', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '5432', # Set to empty string for default. Not used with sqlite3. } }
として、python manage.py syncdb を実行。
すると
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
psycopg2が入っていなとのこと。
psycogp2のインストール
$ sudo pip install psycopg2 $ python manage.py syncdb Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes ...
にて利用可能となる。
postgresql側で確認とすると
$ psql sample sample=> \dt リレーションの一覧 スキーマ | 名前 | 型 | 所有者 ----------+----------------------------+----------+-------- public | auth_group | テーブル | user public | auth_group_permissions | テーブル | user public | auth_message | テーブル | user public | auth_permission | テーブル | user public | auth_user | テーブル | user public | auth_user_groups | テーブル | user public | auth_user_user_permissions | テーブル | user public | django_content_type | テーブル | user public | django_session | テーブル | user public | django_site | テーブル | user (10 行)
作成されたテーブルが確認できる。