お世話になります。
DjangoでPostgreSQLを使おうとしています。
python manage.py runserver 0.0.0.0:3000でローカルサーバを起動しようとすると以下のようなエラーメッセージが出ます。対処方法を教えて下さい。
発生している問題・エラーメッセージ
# python manage.py runserver 0.0.0.0:3000 2020-12-28 10:23:14,416 [INFO] /root/anaconda3/lib/python3.8/site-packages/django/utils/autoreload.py(Line:597) Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). Exception in thread django-main-thread: Traceback (most recent call last): File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection self.connect() File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection connection = Database.connect(**conn_params) File "/root/anaconda3/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL: ロール"root"は存在しません The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/root/anaconda3/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/root/anaconda3/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/root/anaconda3/lib/python3.8/site-packages/django/utils/autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "/root/anaconda3/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run self.check_migrations() File "/root/anaconda3/lib/python3.8/site-packages/django/core/management/base.py", line 453, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "/root/anaconda3/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "/root/anaconda3/lib/python3.8/site-packages/django/db/migrations/loader.py", line 49, in __init__ self.build_graph() File "/root/anaconda3/lib/python3.8/site-packages/django/db/migrations/loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "/root/anaconda3/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations if self.has_table(): File "/root/anaconda3/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 256, in cursor return self._cursor() File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 233, in _cursor self.ensure_connection() File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection self.connect() File "/root/anaconda3/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection self.connect() File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "/root/anaconda3/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection connection = Database.connect(**conn_params) File "/root/anaconda3/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL: ロール"root"は存在しません
尚、Djangoのsettings.pyのDATABASEのセクションには以下のように環境変数を読み取るような設定にしています。
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'private_diary', 'USER':os.environ.get('DB_USER'), 'PASSWORD':os.environ.get('DB_PASSWORD'), } }
試したこと
直接postgresにログインできるか試しましたがだめでした。
# psql -U postgres -d sampledb psql: FATAL: ユーザ "postgres" で対向(peer)認証に失敗しました
以下のコマンドでログインを試みた所成功しました。
# su - postgres 最終ログイン: 2020/12/27 (日) 10:31:32 JST日時 pts/1 -bash-4.2$
補足情報(FW/ツールのバージョンなど)
・CentOS Linux release 7.7.1908 (Core)
・postgres (PostgreSQL) 9.2.24
・Django 2.2.5
回答1件
あなたの回答
tips
プレビュー