Opened 11 years ago

Closed 11 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

Description

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.

Attachments (1)

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

Download all attachments as: .zip

Change History (7)

comment:1 by Tim Graham, 11 years ago

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 by Jon F, 11 years ago

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 by Tim Graham, 11 years ago

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

by Tim Graham, 11 years ago

Attachment: 22417.diff added

comment:4 by Tim Graham, 11 years ago

Has patch: set

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

comment:5 by Claude Paroz, 11 years ago

Triage Stage: AcceptedReady for checkin

As for me, it looks good.

comment:6 by Tim Graham <timograham@…>, 11 years ago

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