Djangoのマイグレーションをとりあえず試してみる
CentOS8を起動。
Vagrantfile
Vagrant.configure("2") do |config| config.vm.box = "bento/centos-8.3" config.vm.network "private_network", ip: "192.168.33.10" config.vm.synced_folder ".", "/vagrant", type:"virtualbox" end
> vagrant up > vagrant ssh
$ sudo dnf install -y python38-devel $ sudo pip3 install django
PostgreSQLをインストール。
$ sudo dnf install -y postgresql-server $ sudo postgresql-setup initdb $ sudo systemctl enable --now postgresql
psycopg2をインストール。
$ sudo dnf install -y libpq-devel gcc $ sudo pip3 install psycopg2
DBを作成。
$ sudo -u postgres psql # CREATE DATABASE hoge_db;
postgresユーザーのパスワードを変更。
# ALTER USER postgres PASSWORD 'postgres'; # \q
md5(パスワード認証)に変更し、再起動。
$ sudo vi /var/lib/pgsql/data/pg_hba.conf ・・・ # "local" is for Unix domain socket connections only local all all md5 # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 ・・・ $ sudo systemctl restart postgresql
Djangoのプロジェクトを作成。
$ cd /vagrant $ django-admin startproject hoge $ cd hoge $ ls hoge manage.py
DBの接続先をPostgreSQLに変更。
$ vi hoge/settings.py ・・・ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'hoge_db', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'localhost', 'PORT': '', } } ・・・
マイグレーションを実行。
$ 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
下記のテーブルが作られた。
$ sudo -u postgres psql hoge_db # \dt List of relations Schema | Name | Type | Owner --------+----------------------------+-------+---------- public | auth_group | table | postgres public | auth_group_permissions | table | postgres public | auth_permission | table | postgres public | auth_user | table | postgres public | auth_user_groups | table | postgres public | auth_user_user_permissions | table | postgres public | django_admin_log | table | postgres public | django_content_type | table | postgres public | django_migrations | table | postgres public | django_session | table | postgres (10 rows)