Search code examples
databasepostgresqlgeometrypostgis

Postgres/Postgis - How to insert geometry in postges (postgis)


I wouldike to insert this value to my database with postgres (postgis) :

INSERT INTO test(
    id, shape)
    VALUES ('test', '<gml:LineString>
            <gml:coordinates>
                -71.16028,42.258729 -71.160837,42.259112 -71.161143,42.25932
            </gml:coordinates>
        </gml:LineString>');');

My table test :

CREATE TABLE test
(
    id character varying(32) COLLATE pg_catalog."default" NOT NULL,
    shape geometry,
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

source : https://postgis.net/docs/ST_GeomFromGML.html

logs ERROR :

ERROR:  parse error - invalid geometry
LINE 3:  VALUES ('test', '
                         ^
HINT:  "
        <g" <-- parse error at position 5 within geometry
SQL state: XX000
Character: 53

Solution

  • According to the documentation you linked to, you want:

    INSERT INTO test(id, shape)
        VALUES ('test', 
                ST_GeomFromGML('<gml:LineString>
                                <gml:coordinates>
                                -71.16028,42.258729 -71.160837,42.259112 -71.161143,42.25932
                                </gml:coordinates>
                                </gml:LineString>')
         );