CLI GuideChannel: postgis sqlgeojson

PostGIS SQL to GeoJSON Converter

Extract PostGIS database layers as GeoJSON files.

This tool is planned for a future release. It will allow you to extract coordinates and spatial attributes from Postgres databases and output them directly as lightweight GeoJSON files, ready for web applications.

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

  • Power real-time web maps with coordinates query structures
  • Convert heavy databases into web-ready JSON files
  • Publish spatial tables as public API endpoints
  • Requires explicit credential parameters to configure connections
  • Large files can cause performance issues in web browsers
  • Drops complex relational properties to maintain web standards

GDAL Direct Equivalent Terminal Command

sh — gdalready
$ogr2ogr -f GeoJSON output.geojson PG:"host=localhost dbname=gis_db user=postgres" -sql "SELECT * FROM parcels LIMIT 100"

When to use this conversion

Use when a PostGIS query needs to feed a web map, a REST API response, or a JavaScript visualisation. GeoJSON is the natural output format; the conversion can be a one-off ogr2ogr export or an on-demand API call.

Technical Details

ogr2ogr connects to PostGIS, runs a SELECT (optionally with a -sql custom query), and writes RFC 7946 GeoJSON. Coordinates are reprojected to WGS 84 if -t_srs EPSG:4326 is passed. For on-demand serving, use ST_AsGeoJSON in a query and stream the result through an HTTP endpoint (PostgREST, custom FastAPI, etc.).
Output file size

GeoJSON is typically 1.5–4× the size of the equivalent PostGIS table data because of JSON text overhead. For web delivery, gzip cuts that back to 30–50% of the original.

Common Errors & Fixes

  • Result too large: a million-row query produces hundreds of MB. Fix: filter with WHERE clauses or paginate via LIMIT/OFFSET; consider vector tiles for very large results.
  • CRS confusion: source SRID 25832 but output expected WGS 84. Fix: ST_Transform(geom, 4326) in the SELECT, or -t_srs at the ogr2ogr level.
  • Properties contain non-JSON-friendly types: bytea, intervals, ranges. Fix: cast to TEXT in the SELECT.
  • Performance slow on first query: missing spatial index on the table. Fix: CREATE INDEX ... USING GIST (geom).

Alternative Tools

  • PostgREST: expose PostgreSQL tables as a REST API returning GeoJSON automatically
  • pg_tileserv for vector tile generation (similar but tile-based)
  • PostGIS query: SELECT ST_AsGeoJSON(geom), name FROM mytable for one-row conversions
  • martin (vector tile server) for production web GIS

Frequently Asked Questions

Can I filter columns before generating files?

Yes. Apply precise SQL queries in the command line to select only the required fields.

Is security guaranteed?

Yes. No database credentials are ever stored or logged by our systems.

What is the standard coordinates reference system used?

The database coordinates project back to standard WGS 84 (EPSG:4326), mapping shapes smoothly.

Related Transformations Map