I am curious how can I find all polygons that are touching with each other but are completely separate from the rest of the polygons is the same dataset, using PostGIS.

For better understanding I uploaded a picture.

enter image description here

  • Are they MultiPolygon data type?
    – Taras
    Aug 30, 2021 at 8:44
  • Yes, but usually they consists of one polygon. Every polygon you see on the picture is separate from others, but every one is defined as MultiPolygon in a database.
    – DrJacoby
    Aug 30, 2021 at 8:49
  • 4
    I don't really understand even with the picture ... Is the question from your picture is : how to find an entire square ? If so, merge / dump and intersect with itself, if no intersections and contains more than 1 origin polygon, it is what you search. Aug 30, 2021 at 8:50
  • PgRouting should work but it requires some pre-processing. Nodes that you cannot access from a start node belong to other islands.
    – user30184
    Aug 30, 2021 at 13:15
  • 1
    I think you may be looking for clustering based on intersection? If so, try this.
    – geozelot
    Aug 30, 2021 at 15:17

2 Answers 2


As suggested in this answer, use ST_ClusterDBSCAN to assign an id to each touching group of polygons:

       ST_ClusterDBSCAN(geom, 0, 1) OVER() AS clst_id
FROM   poly_table;

You can merge touching polygons, see this answer:


This will produce a layer for these "islands".

Then you can simply join back onto the islands from the original data using st_intersects(islands.geom, st_pointonsurface(original.geom))

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