LoginSignup
0
0

More than 1 year has passed since last update.

django.db.utils.OperationalError: FATAL: ロール"<role_name>"は存在しませんと表示された時

Posted at

目的

EC2インスタンスでDjangoのO/M, postgresql@10を使ってマイグレーションを行おうとしたところ、エラーが出たので備忘録。

python manage.py migrate
django.db.utils.OperationalError: FATAL:  ロール"app_admin"は存在しません

結論

存在していないと言われているロールをpostgresql@10シェル内で作成する。

問題

settings.pyのDATABASEの設定で指定している属性が存在していないこと。
私の場合は環境変数から読み込んでいるが、その環境変数に存在していないDB_NAME,DB_USER,DB_PASSWORDを指定していた。

DATABASES = {
    # PostgresSQL
    # get info from .env file
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'HOST': '',
        'PORT': ''
    }
}

具体的には

デフォルトのpsqlというユーザー(ロール)でpostgresqlに接続する

$ sudo -u postgres psql

ロールとデータベースをpostgresql@10シェル内で作成

ロール作成時にpasswordをコンマで囲むことを忘れないように

> CREATE ROLE <role_name> WITH LOGIN PASSWORD '<password>';
# 
> create database <database_name>;
> \q

環境変数を確認

環境変数の設定と先ほど設定したrole_name,password, database_nameが一致しているか確認し、変更がある場合は修正する

$ vi ~/.bash_profile
# ~/.bash_profileの修正を反映
$ source ~/.bash_profile

もう一度マイグレーションしてみる

$ python manage.py migrate
Operations to perform:
  Apply all migrations: account, accounts, admin, auth, contenttypes, diary, django_ses, sessions, sites
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... 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 accounts.0001_initial... OK
  Applying account.0001_initial... OK
  Applying account.0002_email_max_length... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying diary.0001_initial... OK
  Applying django_ses.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying sites.0001_initial... OK
  Applying sites.0002_alter_domain_unique... OK

参考

0
0
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
0
0