[GIS] Export shapefile From PostGIS

convertfwtoolsogr2ogrpgsql2shppostgis

I've a website where I'm giving the functionality of creating and editing line features. I'm using Leaflet-Draw plugin for drawing, PostGIS for storing the data and GeoJSON to display it on map.

My next task is to add the Export functionality. I've search around and found that using ogr2ogr I can convert a PostGIS table to shapefile. But this query works in FWTools, how can I run it on server. Another way I found is to use pgsql2shp. But I don't know where to run this command. I run this in Postgres console and SQL Editor but getting error.

The thing is I want to know

What is the best way in my case to export data to shapefile and how to perform it?

Best Answer

Just install GDAL and run it from Bash / terminal.

ogr2ogr -f "ESRI Shapefile" mydata.shp PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" "mytable"

Or do not even bother with GDAL and just use something like below in terminal.

# pgsql2shp -f myfile -p 5555 my_db roads_table