I'm working on a Python script which needs to use the gdal python module, and not the subprocess call method.
I've been using this, but I can't find any example or good documentation online about how to load a local file to PostgreSQL. Most examples I've seen are the other direction: about exporting from PG to local file. I've tried:
from osgeo import gdal
conn_string = r'PG:"host=localhost dbname=postgres port=5432 user=admin password=xxxx'
in_file = 'polygon.shp'
gdal.VectorTranslate(
out_table, # how do I set a PostgreSQL table name as the destination? How do I pass the PG connection string in?
in_file,
format = 'PostgreSQL',
geometryType = 'POLYGON',
accessMode = 'overwrite',
dstSRS=27700,
reproject=True,
layerCreationOptions=['SCHEMA=schema_name',
'PRECISION=NO',
'OVERWRITE=YES']
)
But get errors like ERROR 1: You must specify at least a db name or a service name or ERROR 1: PostgreSQL driver doesn't currently support database creation. (I just want to create/overwrite a table). Any tips?
-nln new_layer_nameoption. – user2856 Jul 06 '23 at 06:21layerName=somethingoption e.g.VectorTranslate(etc..., layerName="something")– user2856 Jul 06 '23 at 07:20conn_stringviaoptionshttps://gdal.org/api/python/osgeo.gdal.html#osgeo.gdal.VectorTranslateOptions? – PyMapr Jul 06 '23 at 07:40opt = gdal.VectorTranslateOptions(options=[r'PG:"host=localhost, dbname=postgres, port=5432, user=admin, password=xxxx])and then then passopttogdal.VectorTranslate(). This example is untested and the expected string format in the list might be slightly different. – PyMapr Jul 06 '23 at 08:32VectorTranslaterequires 2 arguments. 1: output file/name, and 2: source dataset. So I have to specify the output name. – Theo F Jul 06 '23 at 09:06layerName="something". – user2856 Jul 06 '23 at 09:13PROJ: proj_identifyerrors which occur from runningosgeomodule which has it's own proj.db, on a system which has a core GDAL installation elsewhere. – Theo F Jul 06 '23 at 10:18