I want to export a shapefile into PostGIS via a Python script from QGIS. I’m using QGIS 3.10, PostgreSQL 10 and PostGIS 2.5 on Windows 10. I tried go use qgis:importintopostgis
:
import processing
processing.run("qgis:importintopostgis",{
'INPUT': 'unioncrs',
'DATABASE': "HistoPG",
'SCHEMA': "public",
'TABLENAME': "testunion",
'PRIMARY_KEY': "Nummer"
})
That produces an error message:
“.... There were errors executing the algorithm.”
Interestingly it works with the DB Manager, which seems to be a GUI of qgis:importintopostgis
.
The differences which I can see are the Source and the Target SRIDs. I don’t know how to enter these parameters in qgis:importintopostgis. I’m also not sure if I wouldn’t need to enter username and pasword for the database, but again I don’t see where to enter these parameters in qgis:importintopostgis
.
I also tried `gdal:importvectorintopostgisdatabaseavailableconnections:
processing.run("gdal:importvectorintopostgisdatabaseavailableconnections",{
'DATABASE': "HistoPG",
'INPUT': 'C:/QGIS/Vers1/Results/unioncrs.shp',
'GTYPE': 0,
'SCHEMA': "public",
'TABLE': "testunion"
})
That runs without error message but also doesn’t import a table into PostGIS.
Is there a method which works with Python from QGIS?
I run:
PARAMETERS = {'DATABASE':'HistoPG','INPUT':'C:\\QGIS\\Vers1\\Results\\unioncrs.shp',
'SHAPE_ENCODING':'utf-8','GTYPE':0,'A_SRS':QgsCoordinateReferenceSystem('EPSG:31467'),'T_SRS':None,'S_SRS':None,
'SCHEMA':'public','TABLE':'testunion','PK':'Nummer','PRIMARY_KEY':'','GEOCOLUMN':'geom','DIM':0,'SIMPLIFY':'',
'SEGMENTIZE':'','SPAT':None,'CLIP':False,'WHERE':'','GT':'','OVERWRITE':True,'APPEND':False,
'ADDFIELDS':False,'LAUNDER':False,'INDEX':False,'SKIPFAILURES':False,'PROMOTETOMULTI':True,
'PRECISION':True,'OPTIONS':''}
processing.run("gdal:importvectorintopostgisdatabaseavailableconnections", PARAMETERS)
I then get the following error message:
Traceback (most recent call last): File "C:\PROGRA~2\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 98, in File "C:/PROGRA~2/QGIS3~1.10/apps/qgis-ltr/./python/plugins\processing\tools\general.py", line 106, in run return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File "C:/PROGRA~2/QGIS3~1.10/apps/qgis-ltr/./python/plugins\processing\core\Processing.py", line 181, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.
It creates a table in my PostGIS database. But the table is empty and has only the fields ‚nummer‘ and ‚geom‘.
Do I have to define the fields somewhere in the parameters or do I have to append to a table which has the fields already?
I found the History, that’s good to know. But it doesn’t log when I run DB Manager. And I didn’t find any other plugin to import a vector layer into PostGIS.
Which one did you refer to?
I found the function now in the Processing toolbox. I ran it successfully and used the text from the history in Python. That produced after a while an error message „...There were errors executing the algorithm.“ But it had exported the table into PostgreSQL. The number of features is correct and the table looks alright so far. So I’m not quite sure what went wrong actually.