The PostgreSQL Manual explains how to add the path to binaries to the PATH
environment variable. This procedure depends on your OS distribution and how you installed PostgreSQL and/or PostGIS.
Otherwise, if you want to run a program like shp2pgsql
without adding it to PATH
, the path to the binary needs to be specified:
/usr/local/pgsql/bin/shp2pgsql example.shp public.example > example.sql
There is, yes, using a combination of row_to_json, array_to_json, array_agg and ST_AsGeoJSON. I realize, on rereading your question, that you asked for ogr2ogr approach, but seeing as your source is Postgis, I thought you might appreciate a pure Postgres/Postgis approach. I have used this approach with Google Maps, so it works well if you need to create GeoJSON dynamically, which would be tricker with ogr2ogr.
SELECT row_to_json(featcoll)
FROM
(SELECT 'FeatureCollection' As type, array_to_json(array_agg(feat)) As features
FROM (SELECT 'Feature' As type,
ST_AsGeoJSON(tbl.geom)::json As geometry,
row_to_json((SELECT l FROM (SELECT id, prop1, prop2, ...) As l)
) As properties
FROM your_table As tbL
WHERE st_intersects(geom, st_setsrid(st_makebox2d(st_makepoint(x1, y1),st_makepoint(x2, y2)), srid))
) As feat
) As featcoll;
where there is an optional where clause with an ST_Intersects. In the inner query, you can add to Select id, prop1, etc, to get a list of any properties that you would like to appear in the output GeoJSON.
The original idea for this comes from this BostonGIS blog
EDIT: If you have Postgres 9.4 or higher, use @dbaston's answer. It is so much cleaner.
Best Answer
Just install GDAL and run it from Bash / terminal.
Or do not even bother with GDAL and just use something like below in terminal.