PostGIS: Closest polygon to point

I have .shp file of polygons and single (point) location.

How can I retrieve the closest polygon to that single location? • Any illustration can be a plus – elmo Aug 21 '15 at 13:26
• @elmo Please see my Edit – michael Aug 21 '15 at 13:29
• this question is not really a duplicate of gis.stackexchange.com/questions/14456. That question is about finding closest Geometry in the same table. This question is about finding the closest geometry in a different table. – tinlyx Mar 23 '18 at 9:13

Several ways to do this. One is to find the distance and order ascending.

SELECT geom
FROM polys, points
WHERE points.gid=1 AND ST_DWithin(poly.geom, points.geom, 1000)
ORDER BY ST_Distance(a.geom, b.geom) LIMIT 1;

I added the ST_DWithin call to show how you might limit the number of candidates (it will make use of the index.)

I use ST_ClosestPoint to return the closest point from each polygone, then I calculate the min distance

SELECT foo.* from
(SELECT min(st_distance(a.geom,ST_ClosestPoint(b.geom,a.geom))) from polyg a,point b) foo
• Thanks, but the result is the minimum distance, While I need the geom representing the closest polygon. – michael Aug 21 '15 at 13:59
• seems like your question refer to this link gis.stackexchange.com/questions/14456/… – elmo Aug 21 '15 at 15:06
• could also use ST_ClosestPoint(), and find any polygons which ST_Intersects() the closest point – Steven Kay Aug 21 '15 at 15:36