pgsql2shp - Number of shapes does not match number of records
This error often means that your shapefile (generated by pgsql2shp) is corrupted. The ESRI FAQ suggested common causes such as editing the .DBF file with Excel.
In your case, it's possible that some of the geometries in PostGIS is invalid or NULL. Without actual data, it's difficult to tell. But you may want to check that all geometries in the table are valid with ST_IsValid (and not NULL) in PostGIS before exporting them to a shapefile.
Also, in the following GIS.SE question, there is a script to check whether the number of shapes and attribute entries match:
How to programmatically check if the number of shapes = number of table records?
-- Update --
@wtgeographer From your comments, it seems that the attributes in the DBF file isn't correctly written. I've done some quick experiments to verify:
I exported a table to a shapefile extracting only the gid, geom
columns, just as you did. In my system (QGIS 2.14, Linux, PostGIS 2.3), pgsql2shp
dumped the all the rows; the .shp file is the right size, but QGIS shows 0 records/shapes when the shapefile is loaded.
I suspect this is a bug in handling empty attribute table, because if I exported one non-trivial field such as name
(text), then QGIS can display all the geometry and attributes in the shapefile.
-- Update 2 --
@wtgeographer. You may have to file a bug report and ask the developers to come up with a solution for the empty .DBF problem. I think the gid
field is probably considered special and handled differently. In my experiments, the exported shapefile works even if I rename/copy the gid field using "SELECT gid as myid, geom FROM mytable" in the pgsql2shp
command line.