PostGISGeometry Constructors

ST_MakePolygon

What is ST_MakePolygon?

ST_MakePolygon is a PostGIS function that constructs a POLYGON from a closed LINESTRING shell and an optional array of closed LINESTRINGs used as interior holes.

SQL
1ST_MakePolygon(geometry outerlinestring)geometry
2ST_MakePolygon(geometry outerlinestring, geometry[] interiorlinestrings)geometry

All input rings must be closed (first point equal to last point); an open ring raises an error. All inputs must share the same SRID, which is inherited by the output polygon.

When would you use ST_MakePolygon?

Use ST_MakePolygon to assemble polygons from already-computed ring geometries — for example when you have reconstructed a boundary via ST_MakeLine from an ordered sequence of vertices, or when you are programmatically adding holes to a pre-existing shell.

SQL
1SELECT ST_MakePolygon(
2  ST_MakeLine(boundary_pt ORDER BY seq)
3) AS parcel_geom
4FROM parcel_vertices
5GROUP BY parcel_id
6HAVING COUNT(*) >= 4;

It is also the cleanest way to punch holes into a polygon: build the outer shell as a LINESTRING, then pass an array of hole linestrings as the second argument. For polygons from well-known text, use ST_GeomFromText instead.

FAQs

Why am I getting "shell is not closed" errors?

ST_MakePolygon requires the input LINESTRING to start and end at exactly the same coordinate. Numerical rounding in aggregated data often leaves the ring almost-closed. Use ST_AddPoint(line, ST_StartPoint(line)) to explicitly close it before passing to ST_MakePolygon.

How do I create a polygon with multiple holes?

Pass an array of inner LINESTRINGs as the second argument: ST_MakePolygon(shell, ARRAY[hole1, hole2, hole3]). Each hole must itself be a closed linestring and must lie entirely inside the shell, or the polygon will be invalid.

What is the difference between ST_MakePolygon and ST_Polygon?

ST_MakePolygon accepts one or two arguments (shell with optional holes). ST_Polygon(line, srid) constructs a simple polygon from a closed linestring and an SRID, but does not accept holes. Use ST_MakePolygon for any case involving interior rings.

Does ST_MakePolygon validate the result?

No. The function assembles the rings into a polygon but does not check for self-intersection, hole containment, or ring orientation. Call ST_IsValid(poly) after construction and repair with ST_MakeValid if needed.