I added a new column by:

SELECT AddGeometryColumn('table_name', 'column_name', 4326, 'POLYGON', 2, false);

Which means 2D polygon in WGS84.

Then I inserted a new polygon into specific row by:

UPDATE table_name
SET column_name = ST_MakePolygon(ST_GeomFromText('LINESTRING(lon1 lat1, lon2 lat2 ... lon1 lat1)'))
WHERE gid=26;

The result is "violates check constraint enforce_srid" error. What am I doing wrong?

  • 2
    You didn't specify the SRID parameter in ST_GeomFromText, which is basically what the error message said.
    – Vince
    Jul 16 '16 at 1:57
  • Where I need to specify? In the new column I wrote the 4326
    – michael
    Jul 16 '16 at 1:58
  • 3
    Look at the documentation. You need to specify an SRID in every ST_GeomFromText or it will use 0, which violates the constraint. Do you realize you're wasting time by generating a line from WKT then converting to polygon when you could just generate a polygon in one step?
    – Vince
    Jul 16 '16 at 2:43
  • How can I generate polygon in one step?
    – michael
    Jul 16 '16 at 13:19

You created a geometry colum with SRID 4326 but then you are trying to put in a polygon that does not identify an SRID. You just need to pass the polygon to ST_SetSRID() like this:

UPDATE table_name
SET column_name = ST_SetSRID(ST_MakePolygon(ST_GeomFromText('LINESTRING(lon1 lat1, lon2 lat2 ... lon1 lat1)')),4326)
WHERE gid=26;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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