0

I am trying to write a script to import/append mutiple layers into an existing PostGIS database. I am trying to use the GDAL/OGR toolbox algo "Import Vector into PostGIS database (new connection)".

Here's my code:

vl  =QgsVectorLayer("D:/Users/Ravi_narayanan/Documents/VDLF/test2.shp","test2","ogr")

processing.runalg('gdalogr:importvectorintopostgisdatabasenewconnection', vl, 3, vl.crs().authid(),vl.crs().authid(),vl.crs().authid(), "172.22.140.42", "5432", "ivy01", "YVI", "ivy01!", "TEST1", "new", "ID","ID", "geom", 0,"","","0,1,0,1",False,"","20000",False, True, False, False, True, False,False,False,"")

This gives me the following error:

GDAL execution console output FAILURE:

        Unable to open datasource `port=5432' with the following drivers.

The list of drivers contains PostgreSQL. The connection to the database works fine using a standard PyQGIS uri object.

When i tried using the toolbox GUI. The GDAL/OGR console command is as follws:

ogr2ogr.exe -progress --config PG_USE_COPY YES -f PostgreSQL PG:" "host=172.22.149.42 port=5432 dbname=YVI password=ivy01! active_schema=TEST1 user=ivy01" " -lco DIM=2 D:\Users\Ravi_narayanan\Documents\VDLF\test4.shp test4 -overwrite -nlt POINT -lco GEOMETRY_NAME=geom -lco FID=id -nln new -spat 480030.5 6691467.0 484106.5 6696344.0 -nlt PROMOTE_TO_MULTI

There is a " "after PostgreSQL PG: instead of a " when compared with the GUI of Import with available connections.

i'm using QGIS 2.14.11 and the number of parameters is not the same as in the link JGH gave.

ALGORITHM: Import Vector into PostGIS database (new connection)
INPUT_LAYER <ParameterVector>
GTYPE <ParameterSelection>
A_SRS <ParameterCrs>
T_SRS <ParameterCrs>
S_SRS <ParameterCrs>
HOST <ParameterString>
PORT <ParameterString>
USER <ParameterString>
DBNAME <ParameterString>
PASSWORD <ParameterString>
SCHEMA <ParameterString>
TABLE <ParameterString>
PK <ParameterString>
PRIMARY_KEY <parameters from INPUT_LAYER>
GEOCOLUMN <ParameterString>
DIM <ParameterSelection>
SIMPLIFY <ParameterString>
SEGMENTIZE <ParameterString>
SPAT <ParameterExtent>
CLIP <ParameterBoolean>
WHERE <ParameterString>
GT <ParameterString>
OVERWRITE <ParameterBoolean>
APPEND <ParameterBoolean>
ADDFIELDS <ParameterBoolean>
LAUNDER <ParameterBoolean>
INDEX <ParameterBoolean>
SKIPFAILURES <ParameterBoolean>
PROMOTETOMULTI <ParameterBoolean>
PRECISION <ParameterBoolean>
OPTIONS <ParameterString

1 Answer 1

1

The issue is in

processing.runalg('gdalogr:importvectorintopostgisdatabasenewconnection', vl, 3, vl.crs().authid(),vl.crs().authid(),vl.crs().authid(), "172.22.140.42", "5432" ...

because the CRS is written 3 times, while the algorithm expects only 2 (source + target). If all other params are OK, it should work by removing the extra CRS.

You can check the parameters here

1
  • It throws the same error when give an empty string Commented Jan 24, 2018 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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