I'm trying to export a simple PostGIS table loaded in QGIS to a geopackage file.
Here's an example:
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.layerName = "_".join(myLayer.name().split(' '))
geo_package = 'my_gpkg.gpkg'
if not os.path.exists(geo_package):
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
else:
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
res, text = QgsVectorFileWriter.writeAsVectorFormatV2(myLayer, geo_package, QgsCoordinateTransformContext(), options)
if res == QgsVectorFileWriter.NoError:
print("Successfull export of layer {}".format(myLayer))
else:
print("Fail error {}:{}".format(res, text))
It works fine for Vector layers but for a simple PostGIS table, I get the Error code 8 with no Error message.