Functions / PostGIS / ST_Polygon
PostGISGeometry Constructors

ST_Polygon

What is ST_Polygon?

ST_Polygon is a PostGIS function that constructs a simple POLYGON from a closed LINESTRING and an SRID. It is a convenience constructor for the common case of a hole-less polygon built from a ring of vertices.

SQL
ST_Polygon(geometry lineString, integer srid)geometry

The input must be a closed LINESTRING; if it is not, the function raises an error. The output inherits the supplied SRID. Unlike ST_MakePolygon, this function does not accept an interior-ring array.

When would you use ST_Polygon?

Use ST_Polygon when you have a closed ring in hand (perhaps built from ST_MakeLine with an explicit first/last closure) and need to promote it to a polygon with a specific SRID:

SQL
1SELECT ST_Polygon(
2  ST_MakeLine(ARRAY[
3    ST_Point(0,0,4326), ST_Point(10,0,4326),
4    ST_Point(10,10,4326), ST_Point(0,10,4326), ST_Point(0,0,4326)
5  ]),
6  4326
7) AS square;

For polygons with holes, use ST_MakePolygon(shell, holes) instead. For parsing well-known text, use ST_GeomFromText or ST_PolygonFromText.

FAQs

What is the difference between ST_Polygon and ST_MakePolygon?

ST_Polygon(line, srid) takes exactly two arguments: a closed linestring and an SRID. ST_MakePolygon accepts one or two arguments — a shell and an optional array of hole linestrings — but does not take an SRID; the SRID is inherited from the input linestring.

Can ST_Polygon create polygons with holes?

No. Use ST_MakePolygon(shell, ARRAY[hole1, hole2, ...]) for polygons with interior rings.

Why do I get "geometry must be a closed LineString"?

The first and last vertices of the linestring must be exactly equal. Rounding from aggregated data often leaves the ring not-quite-closed. Close it explicitly: ST_AddPoint(line, ST_StartPoint(line)).

Does ST_Polygon validate the output?

No. It assembles the polygon but does not check for self-intersections. Always follow construction with ST_IsValid for user-supplied or machine-generated rings, and repair with ST_MakeValid if needed.