简体   繁体   中英

Django error: django.db.utils.OperationalError: FATAL: Peer authentication failed for user

I'm setting up django on amazon web and I'm getting an issue connecting to the database. I created the database and checked to see it exists:

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

and the user also exists

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

here's my database settings

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.
    }
}

when I try runserver I get an error

(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"

On pg_hba.conf change

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          peer

to

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          md5

And restart the postgresql server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM