[gdal-dev] gdal 3.11 python API and PostgreSQL driver

Thomas Szczurek-Gayant thomas.szczurek at gmail.com
Fri Aug 22 23:08:24 PDT 2025


Ok solved it.

What I was trying to do was simply to copy a csv into a Postgres 
database. (db and schema already existing, not the table)


The problem was (in terminal)

old way :

  ogr2ogr PG:"dbname='insee' port=5432 host='localhost' user='thomas' 
password='xxxxxxxx' schemas='wip'" test_file.csv

works


new way :

gdal vector convert test_file.csv PG:"dbname='insee' port=5432 
host='localhost' user='thomas' password='xxxxxxxx' schemas='wip'"

don't work, I need to add the --append option, even to create the table 
from scratch (but is that the intended way ?)


Another problem was that with the Python API, with the :

format=PostgreSQL option

it won't be enough, I still need to specify PG: before my connection 
string or gdal will search for a file or directory named as the 
connection string.


Thanks a lot !



Le 22/08/2025 à 19:21, Even Rouault a écrit :
>
> Thomas,
>
> are you sure you really used --overwrite in the terminal and not 
> --overwrite-layer ? The latter one is the one you want to use in this 
> scenario of opening an existing datasource and overwriting its layer. 
> And in python this will be overwrite_layer=True
>
> Even
>
> Le 22/08/2025 à 19:13, Thomas Szczurek-Gayant via gdal-dev a écrit :
>>
>> Hi,
>>
>>
>> I've a question with the python API of gdal 3.11 new programs and 
>> PostgreSQL driver.
>>
>>
>> I can run this in a terminal
>>
>> gdal vector convert --overwrite base-cc-evol-struct-pop.csv 
>> PG:"dbname='insee' port=5432 host='localhost' user='thomas' 
>> password='xxxxxx' schemas='wip' tables='rp_population_import'"
>>
>>  and everything is working fine.
>>
>> But if I try from the python API with
>>
>> gdal.Run("vector", "convert", format="PostgreSQL", overwrite=True, 
>> input="base-cc-evol-struct-pop.csv", output="dbname='insee' port=5432 
>> host='localhost' user='thomas' password='xxxxxxxx' schemas='wip' 
>> tables='rp_population_import'")
>>
>> Python throw this error as if gdal/postgres actually try to create a 
>> new db:
>>
>> ERROR 1: PostgreSQL driver doesn't currently support database creation.
>> Please create database with the `createdb' command.
>> ERROR 1: PostgreSQL driver failed to create dbname='insee' port=5432 
>> host='localhost' user='thomas' password=XXXXXXXXXXXX schemas='wip' 
>> tables='rp_population_import'
>> Traceback (most recent call last):
>>   File "/home/thomas/repos_git/bd_insee/python/__main__.py", line 63, 
>> in <module>
>>     gdal.Run("vector", "convert", format="PostgreSQL", 
>> overwrite=True, input="base-cc-evol-struct-pop.csv", 
>> output="dbname='insee' port=5432 host='localhost' user='thomas' 
>> password='xxxxxx' schemas='wip' tables='rp_population_import'")
>> ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>   File 
>> "/home/thomas/miniforge3/envs/bd_insee/lib/python3.13/site-packages/osgeo/gdal.py", 
>> line 3353, in Run
>>     raise RuntimeError("Algorithm.Run() failed: %s" % GetLastErrorMsg())
>> RuntimeError: Algorithm.Run() failed: PostgreSQL driver failed to 
>> create dbname='insee' port=5432 host='localhost' user='thomas' 
>> password=XXXXXXXXXXXX schemas='wip' tables='rp_population_import'
>>
>>
>> Does anyone have a clue or it's me doing it bad ?
>>
>> Thanks !
>>
>>
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
> -- 
> http://www.spatialys.com
> My software is free, but my time generally not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20250823/bbe3074c/attachment.htm>


More information about the gdal-dev mailing list