Take the 2-minute tour ×  My data is in a PostGIS database. I
would like to generate a shapefile from a query. How can I do
that?  share|improve this question add comment  2 Answers  up
vote 17 down vote accepted  The recommended way to do this is
using the pgsql2shp utility, which should be installed with
PostGIS. Note that you must include the geometry column in the
query.  $ pgsql2shp -f <path to output shapefile> -h <hostname>
-u <username> -P <password> databasename "<query>"  Example
(creates qds_cnt.shp in current directory):  $ pgsql2shp -f
qds_cnt -h localhost -u postgres -P password gisdb "SELECT
sp_count, geom FROM grid50_rsa WHERE province = 'Gauteng'"  Done
(postgis major version: 2). Output shape: Polygon Dumping:
XXXXXXXXXXXXXXXXXXXX [1947 rows].  If you wanted to save a whole
table as a shapefile, just use the table name as the query.  You
can also use the ogr2ogr utility, but it has more dependencies
so should not be the first option. If you are determined, the
equivalent command would be:  $