﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22417	"Django 1.7 DB connection fails due to requesting database ""postgres"""	Jon F <jon@…>	Tim Graham	"I'm trying Django 1.7 Beta 1 with the PostgreSQL back end.

When I run my unit tests under Django 1.7b1, one of the very first things it does is try to connect to the ""postgres"" database, using the credentials configured in my settings.  But in my PostgreSQL configuration, that user doesn't have access to the ""postgres"" database, so it fails with an error message.  Django 1.6 does what I would expect: it only connects to the database that's configured in DATABASES.

The error message is:

{{{
Creating test database for alias 'default'...
Traceback (most recent call last):
  File ""./manage.py"", line 8, in <module>
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"", line 427, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py"", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py"", line 71, in execute
    super(Command, self).execute(*args, **options)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py"", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File ""/usr/local/lib/python2.7/dist-packages/django/test/runner.py"", line 147, in run_tests
    old_config = self.setup_databases()
  File ""/usr/local/lib/python2.7/dist-packages/django/test/runner.py"", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File ""/usr/local/lib/python2.7/dist-packages/django/test/runner.py"", line 297, in setup_databases
    verbosity, autoclobber=not interactive)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/creation.py"", line 354, in create_test_db
    self._create_test_db(verbosity, autoclobber)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/creation.py"", line 409, in _create_test_db
    with self._nodb_connection.cursor() as cursor:
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py"", line 159, in cursor
    cursor = utils.CursorWrapper(self._cursor(), self)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py"", line 129, in _cursor
    self.ensure_connection()
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py"", line 124, in ensure_connection
    self.connect()
  File ""/usr/local/lib/python2.7/dist-packages/django/db/utils.py"", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py"", line 124, in ensure_connection
    self.connect()
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py"", line 113, in connect
    self.connection = self.get_new_connection(conn_params)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py"", line 135, in get_new_connection
    return Database.connect(**conn_params)
  File ""/usr/lib/python2.7/dist-packages/psycopg2/__init__.py"", line 179, in connect
    connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  Peer authentication failed for user ""ligada""
}}}

When you try to connect to a PostgreSQL database, it searches pg_hba.conf using your connection method, username, and the name of the database you're connecting to, to decide whether or not to allow the connection, and whether to require a password or not.  I gave my DB user the minimum possible rights - that's just good security practise.  So that user was only allowed to connect to my app database & the app_test database, it wasn't allowed to connect to the ""postgres"" database.  Adding an extra line to pg_hba.conf to allow that user to access the ""postgres"" database has worked around this issue.

I am guessing that this behaviour is related to the new migrations code?  Is this a bug?  Can this be fixed somehow in the Django DB code?  Or is this an essential part of the new migrations code, and everyone is going to have to update their PostgreSQL settings?

If this isn't fixed in the Django 1.7 release, it should be added to the release notes, since this is a change in behaviour, and the error message is confusing.
"	Bug	closed	Documentation	1.7-beta-1	Release blocker	fixed			Ready for checkin	1	0	0	0	0	0
