Opened 5 years ago

Closed 5 years ago

#22417 closed Bug (fixed)

Django 1.7 DB connection fails due to requesting database "postgres"

Reported by: Jon F <jon@…> Owned by: Tim Graham
Component: Documentation Version: 1.7-beta-1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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 "./", line 8, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 427, in execute_from_command_line
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 419, in execute
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/local/lib/python2.7/dist-packages/django/test/", line 147, in run_tests
    old_config = self.setup_databases()
  File "/usr/local/lib/python2.7/dist-packages/django/test/", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/test/", line 297, in setup_databases
    verbosity, autoclobber=not interactive)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 354, in create_test_db
    self._create_test_db(verbosity, autoclobber)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 409, in _create_test_db
    with self._nodb_connection.cursor() as cursor:
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 159, in cursor
    cursor = utils.CursorWrapper(self._cursor(), self)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 129, in _cursor
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 124, in ensure_connection
  File "/usr/local/lib/python2.7/dist-packages/django/db/", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 124, in ensure_connection
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", 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/", line 135, in get_new_connection
    return Database.connect(**conn_params)
  File "/usr/lib/python2.7/dist-packages/psycopg2/", 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.

Attachments (1)

22417.diff (1.3 KB) - added by Tim Graham 5 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years ago by Tim Graham

Component: MigrationsTesting framework
Triage Stage: UnreviewedAccepted

This is actually due to #16969, not migrations. I guess I came to the wrong conclusion that all users should have access to the postgres database. Is the permissions something you can fix? (in which case the fix may be simply to document the requirement as you noted)

comment:2 Changed 5 years ago by Jon F

Yes, this is something that can be fixed by editing the PostgreSQL configuration (the pg_hba.conf file). And if it's only needed for running the unit tests, then it's probably not a big deal to change it. Most people who care about security are going to be using different database servers for test and production, and they only have to change permissions on the test server.

comment:3 Changed 5 years ago by Tim Graham

Component: Testing frameworkDocumentation
Owner: changed from nobody to Tim Graham
Status: newassigned

Changed 5 years ago by Tim Graham

Attachment: 22417.diff added

comment:4 Changed 5 years ago by Tim Graham

Has patch: set

Jon, anything you would add or change in the proposed patch?

comment:5 Changed 5 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin

As for me, it looks good.

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 0086c9eb48101b6dabefa7f9195ac7d197780f09:

[1.7.x] Fixed #22417 -- Added additional documentation for refs #16969.

Thanks Jon Foster for the report.

Backport of 1b3a3fc1e4 from master

Note: See TracTickets for help on using tickets.
Back to Top