CLI GuideChannel: geojsonpostgis sql

GeoJSON to PostGIS SQL Converter

Import web JSON coordinates into central Postgres databases.

Seamlessly migrate web geometries to central servers. This upcoming tool parses GeoJSON datasets and writes them to PostGIS as spatially-indexed database tables, complete with attributes.

Indirect Mapping Execution Required

Because database connection attributes or local attributes parsing is required, direct web streams are disabled. Use the GDAL CLI script below for precise terminal conversions.

Specification Rules

  • Save user-generated drawing shapes from web maps directly to server databases
  • Consolidate crowdsourced map layers in central enterprise systems
  • Batch-import spatial JSON lists to high-capacity server instances
  • JSON properties must match target database field types
  • Subject to column width limitations inside target architectures
  • Requires active spatial metadata profiles in the target database

GDAL Direct Equivalent Terminal Command

sh — gdalready
$ogr2ogr -f PostgreSQL PG:"dbname=gis_db user=admin" input.geojson

When to use this conversion

Use when crowdsourced edits, API submissions, or front-end-generated geometry need to land in a PostGIS master database. Typical case: a web form lets users draw a polygon, the result is serialised as GeoJSON, and the backend persists it via this conversion.

Technical Details

ogr2ogr reads the GeoJSON FeatureCollection and writes to a PostgreSQL table with a geometry column. Property types are inferred from the first feature. CRS is set from the GeoJSON (default WGS 84 per RFC 7946) and projected via -t_srs if needed. For programmatic single-row inserts, ST_GeomFromGeoJSON inside a regular INSERT is more flexible.
Output file size

PostgreSQL storage is typically 30–60% of the GeoJSON size. Binary geometry storage is far more compact than JSON text representation.

Common Errors & Fixes

  • CRS mismatch: GeoJSON in WGS 84 inserted into a table with SRID 25832 column. Fix: explicit ST_Transform during insert or set -t_srs.
  • Column type collision: target table has integer for a column where GeoJSON gives strings. Fix: clean source data or alter table to TEXT.
  • Topology error: invalid polygon (self-intersection) rejected by PostGIS. Fix: ST_MakeValid(geom) in the insert.
  • Performance slow on bulk insert: indexes still being built per row. Fix: drop indexes before insert, recreate after.

Alternative Tools

  • ST_GeomFromGeoJSON in plain SQL: INSERT ... ST_GeomFromGeoJSON('<json>') for single rows
  • PostgREST automated PATCH/POST endpoints accepting GeoJSON payloads
  • QGIS: open .geojson, drag into PostGIS connection
  • ogr2ogr CLI: ogr2ogr -f PostgreSQL PG:... input.geojson

Frequently Asked Questions

How are JSON properties mapped to columns?

GDAL automatically reads root key properties, creating matching column names in the PostgreSQL tables.

Can I append data to existing tables?

Yes, you can configure the importer to append layers to existing tables using database write options.

Does this require coordinate system adjustments?

Coordinates map automatically, but you can configure the importer to reproject coordinates to match your database defaults.

Related Transformations Map