Opened 9 years ago

Closed 9 years ago

#24862 closed Cleanup/optimization (fixed)

Patch for GeoDjango to improve error message if database connection is bad

Reported by: brycenesbitt Owned by: Sylvain Fankhauser
Component: GIS Version: dev
Severity: Normal Keywords: PostGIS, GeoDjango, GIS, Usability
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description (last modified by brycenesbitt)

For a project with GeoDjango if the database is completely down, you'll get exception:

django.core.exceptions.ImproperlyConfigured: Cannot determine PostGIS version for database "umap" . GeoDjango requires at least PostGIS version 1.3. Was the database created from a spatial database template?

This patch first does a trivial database connection to flesh out the real exception, before checking the PostGIS version number:

***************
*** 246,256 ****
          # comprising user-supplied values for the major, minor, and
          # subminor revision of PostGIS.
- 
-         # First connect to the database trivially
-         # This prevents raising a GeoDjango exception below if the connection is bad
-         with self.connection.temporary_connection() as cursor:
-             cursor.execute('SELECT version()')
- 
-         # Now check PostGIS
          if hasattr(settings, 'POSTGIS_VERSION'):
              version = settings.POSTGIS_VERSION
--- 246,249 ----
***************
*** 260,265 ****
              except DatabaseError:
                  raise ImproperlyConfigured(
!                     'Cannot determine PostGIS version for database "%s" '
!                     'using command "SELECT postgis_lib_version()". '
                      'GeoDjango requires at least PostGIS version 1.3. '
                      'Was the database created from a spatial database '
--- 253,257 ----
              except DatabaseError:
                  raise ImproperlyConfigured(
!                     'Cannot determine PostGIS version for database "%s". '
                      'GeoDjango requires at least PostGIS version 1.3. '
                      'Was the database created from a spatial database '

Change History (5)

comment:1 by brycenesbitt, 9 years ago

Description: modified (diff)

comment:2 by Claude Paroz, 9 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted
Version: 1.8master

Good suggestion. Could you attach a patch against master, either as a Github pull request, or as a git diff output?

comment:3 by Sylvain Fankhauser, 9 years ago

Owner: changed from nobody to Sylvain Fankhauser
Status: newassigned

comment:4 by Sylvain Fankhauser, 9 years ago

Pull request is available here: https://github.com/django/django/pull/4796/

Note that:

  • I didn't include tests for this trivial change
  • I wasn't able to reproduce the issue since it looks like that on newer PostGIS versions PostGIS functions are always available

comment:5 by Claude Paroz <claude@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 0a899157:

Fixed #24862 -- Improved PostGIS database connection error message

Thanks brycenesbitt for the patch.

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