2

I am running PostgreSQL/PostGIS version 12 (win10/64 bit) attempting to import shapefiles by means of shp2pgsql.exe. Really nothing happens. The GUI only blinks a fraction of a second. Then nothing. What am I doing wrong?

  • 1
    That's a command-line executable, not a GUI app. – Vince Feb 19 at 13:40
  • i would suggest posting screenshots or exact commands of what you are doing – ziggy Feb 19 at 13:42
4

The GUI is a different exe than the command line one.

The command line tool is at C:\Program Files\PostgreSQL\12\bin\shp2pgsql.exe

while the GUI is at C:\Program Files\PostgreSQL\12\bin\postgisgui\shp2pgsql-gui.exe

2

If you want a GUI to import shapefiles I recommend to use DB Manager (comes with QGIS) rather than shp2pqsql. DB Manager is a really efficient plugin which permits to import all kind of data (shapefiles, geojson, tif...) into PostGIS tables.

If you want to use the PyQGIS option, here's an example on how to automatize your PostGIS imports

from qgis.core import QgsVectorLayer, QgsVectorLayerExporter, QgsWkbTypes, QgsCoordinateReferenceSystem

layer_path = 'path/to/my/layer'
vlayer = QgsVectorLayer(layer_path, 'ogr') # Loading dataset

# Checking the geometry type
geometry_test = QgsWkbTypes.displayString(int(vlayer.wkbType()))
check_format = False

if geometry_test in 'Line':
    format = 'MultiLineString'
    check_format = True
elif geometry_test in 'Polygon':
    format = 'MultiPolygon'
    check_format = True
elif geometry_test in 'Point':
    format = 'MultiPoint'
    check_format = True
else:
    print('Unable to find a correct geometry type')

# Exporting the layer to postgis, replace db_arg with your db informations
# The key parameter in db_arg is optionnal because id will be created if it doesn't already exist
if check_format:
     db_arg = "host=localhost dbname='mydb' port=5432 user='postgres' password='mypassword' key='name_of_the_primary_key_column' type='" + format + "' table='the_name_for_the_postgis_table' (geom)"
     QgsVectorLayerExporter.exportLayer(vlayer, db_arg, 'postgres', QgsCoordinateReferenceSystem('4326'))

You just have to change the layer_path variable, the db_arg variable and optionally the QgsCoordinateReferenceSystem to put the projection you want for the import and that's it !

  • 1
    Yes I forgot to specify it thanks ! – Etienne Conte Feb 19 at 15:10
  • Thanks. I am trying via QGIS DB Manager. It fails leaving me with the following (sorry some of it is in Danish, but it should tell most of the story): ERROR 2: Oprettelse af datakilde "public"."trackTest" mislykkedes: ERROR: function addgeometrycolumn(unknown, unknown, unknown, integer, unknown, integer) does not exist LINE 1: SELECT AddGeometryColumn('public','trackTest',NULL,25833,'MU... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. – Hans Skov-Petersen Feb 20 at 14:23
  • Check the default parameter when you import your data (maybe there's a problem with those default parameters). Also, PostGIS can't import your data if there's geometry issues, so if it doesn't work you can try the Fix Geometries tool from the QGIS processing toolbox – Etienne Conte Feb 20 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.