# Extracting single boundary from multipolygon in PostGIS

I am trying to find a single external boundary either polygon or line from the multipolygon using `st_boundary` in PostGIS query. The function doesn't work. What could be the issue?

• Can you please show us some graphics/code/error reports that you have – Taras Jun 4 at 8:12
• `select st_boundary(geom) from (select polygon(geo_gem_05)::geometry As geom) As f;` This is the query where geo_gem_05 is a multipolygon. – Karthik Jun 4 at 8:55
• Edit the question, don't put clarification in comments. And, please state how it is not working. – John Powell Jun 4 at 9:42
• geo_gem_05 is the polygon which I am defining here – Karthik Jun 4 at 10:02
• @Karthik literally put your code comment in the question – ziggy Jun 4 at 13:27

I do not understand your question a little and therefore I will offer you two possible answers, use the one that answers your question

Option 1 - to find all boundaries, run the query:

`SELECT ST_ExteriorRing((ST_Dump(geom)).geom) as geom FROM geo_gem_05;`

Option 2 - to find only a common external boundary, run the query:

`SELECT ST_ExteriorRing((ST_Dump(ST_Union(geom))).geom) as geom FROM geo_gem_05;`

for experiment you can replace "ST_ExteriorRing" for "ST_Boundary"

Good luck ...

What do you mean by "The function doesn't work"? It does work for me.

``````SELECT ST_AsText(ST_Boundary(ST_GeomFromText('MULTIPOLYGON ((( 340 520, 400 460, 200 420, 340 520 )), (( 420 420, 520 300, 340 320, 420 420 )))')));

Result:
MULTILINESTRING((340 520,400 460,200 420,340 520),(420 420,520 300,340 320,420 420))
``````

The result is a multilinestring with two closed linestrings, one for each member in the multipolygon. That is the correct result by the definition of a boundary https://postgis.net/docs/ST_Boundary.html. Please explain if you are after something else.

I got the answer by applying Cyril's query.

``````SELECT ST_ExteriorRing((ST_Dump(ST_Union(geom))).geom) as geom FROM geo_gem_05;
or
SELECT ST_Boundary((ST_Dump(ST_Union(geom))).geom) as geom FROM geo_gem_05;
``````

The resultant output is given below