Django错误:django.db.utils.OperationalError:致命错误:用户的对等身份验证失败

时间:2017-01-20 07:07:27

标签: python django

我正在亚马逊网上设置django,我遇到了连接数据库的问题。我创建了数据库并检查它是否存在:

postgres=# CREATE DATABASE kbuzz;
ERROR:  database "kbuzz" already exists

并且用户也存在

postgres=# SELECT 1 FROM pg_roles WHERE rolname='kbuzz_web'
postgres-# ;
 ?column? 
----------
        1
(1 row)

这是我的数据库设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'kbuzz',                      # Or path to database file if using sqlite3.
        'USER': 'kbuzz_web',

                          # Not used with sqlite3.
        'PASSWORD': '<hidden>',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

当我尝试运行runserver时出现错误

(djangoenv) ubuntu@ip-172-31-16-133:~/webapps/kenyabuzz$ python manage.py runserver
Performing system checks...

System check identified some issues:

WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
    HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.

System check identified 1 issue (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x7f222281a320>
Traceback (most recent call last):
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 222, in wrapper
    fn(*args, **kwargs)
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    self.check_migrations()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 159, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
    self.build_graph()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 179, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.get_table_list(self.connection.cursor()):
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 167, in cursor
    cursor = utils.CursorWrapper(self._cursor(), self)
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 138, in _cursor
    self.ensure_connection()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 122, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 134, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/ubuntu/webapps/djangoenv/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  Peer authentication failed for user "kbuzz_web"

1 个答案:

答案 0 :(得分:1)

在pg_hba.conf上更改

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          peer

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          md5

然后重新启动postgresql服务器