I've been using PostGIS via GeoDjango (Django v3.0.7), happily until just now. I have a query (Django .filter()) that looks like this:

.filter(point__distance_lte=(dlPt, D(m=CloseRadius)))

when it invokes the PostgreSQL query I get this error:

django.db.utils.ProgrammingError: function ST_DistanceSphere(geometry, geometry) does not exist

dlPt is indeed a Point, <class 'django.contrib.gis.geos.point.Point'>, and so are the point attributes in the objects being searched.

Any ideas what the issue might be?

  • Have you created PostGIS extension in the database? – Ahsan Mukhtar Sep 28 '20 at 19:03
  • If created, what version of PostGIS are you using? – Ahsan Mukhtar Sep 28 '20 at 19:05
  • Looks like distance_lte uses ST_Distance_Sphere (note the _; not sure if typo, though), which is deprecated in PostGIS 3.x. Test with direct SQL; if that's the case, it's tivial to write a wrapper function. Also, if possible, use dwithin (it calls the index driven ST_DWithin, which is much faster). – geozelot Sep 28 '20 at 19:17

Function st_distancesphere is avaialble for PostGIS version 2.2 and greater. So make sure that while using PostGIS with Geodjango it has a version that is greater then 2.2, otherwise it won't be able to find it in the database.


And you can check your PostGIS version by using this query

SELECT PostGIS_Version();
  • yup, that was it! SELECT PostGIS_Version() returns: "2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1". thanks for your help! – rikb Sep 29 '20 at 15:54
  • No problem! Glad to help! – Ahsan Mukhtar Sep 29 '20 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.