Arus23
Arus23

Reputation: 89

How to insert polygon into database with PostGIS extension

I have a table with fields created in such way:

CREATE TABLE buildings(id SERIAL PRIMARY KEY, geom GEOMETRY, name VARCHAR(30));

and I want to insert simple 2D building into it with following coordinates 8 4, 10.5 4, 10.5 1.5, 8 1.5

INSERT INTO buildings(id, geom, name)
VALUES
(1, ST_GeomFromText('POLYGON(8 4, 10.5 4, 10.5 1.5, 8 1.5)'), 'BuildingA');

for some reason I get an error

ERROR:  parse error - invalid geometry
HINT:  "POLYGON(8 4" <-- parse error at position 11 within geometry

How can I insert that polygon into the table?

Upvotes: 1

Views: 2019

Answers (1)

Blue Star
Blue Star

Reputation: 1952

ST_GeomFromText takes a WKT as its argument. If you look at the WKT format, you'll see that you need another pair of brackets for your polygon. You'll also want the last point in the polygon to match the first one.

INSERT INTO buildings(id, geom, name)
VALUES
(1, ST_GeomFromText('POLYGON((8 4, 10.5 4, 10.5 1.5, 8 1.5, 8 4))'), 'BuildingA');

Upvotes: 6

Related Questions