I would like to store GeoJSON data into PostgreSQL database.
WITH data AS (SELECT '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}'::json AS fc)
SELECT
row_number() OVER () AS gid,
ST_AsText(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM data
) AS f;
I can query but I need to save data as a table. Does anyone know how to store this kind data into a table in PostgreSQL.?
Reference to Storing GeoJSON FeatureCollection to PostgreSQL with PostGIS?
This reference does only query but I need save the layer as a table.
geometrytype, not text) and a jsonb properties field. Then, from your current query, remove thegidgeneration and also removest_asText. At last, addINSERT INTO mytable (geometry, properties)just before theSELECTstatement. – JGH Dec 18 '19 at 13:13