Search code examples
c++postgresqlgdal

How do I open postGIS table with gdal in C++?


I'm completely new in gdal with C++, and want to open postgreSQL table(with geometry) with gdal API. I cannot find how to do that in tutorial of gdal.org. Below is what i was trying now. How can I do open table with GDALOpenEx for postgreSQL?

GDALAllRegister();

GDALDataset* poDS;


std::string source = "PG:host=111.111.111.111 port=5432 dbname=db user=user password=password";
poDS = (GDALDataset*) GDALOpenEx(source.c_str(), GDAL_OF_VERBOSE_ERROR | GDAL_OF_VECTOR, NULL, NULL, NULL);

ERROR 4: PG:host=host port=5432 dbname=db user=user password=XXXX No such file or directory


Solution

  • I was also puzzled with this question for serveral hours. I have used the same kind of connection string. This is enough to connect the postgresql.If this is not useful,Please Check something else. Enable the postgresql when you compile the gdal(in the nmake.opt file open PostGIS Libraries colume,set you libpq.lib file and include directory). code:

        GDALAllRegister();
        const char* path ="PG:dbname=db host=localhost port=5432 user=postgres 
        password=password";
        GDALDataset *pDos =nullptr;
        pDos = (GDALDataset *)GDALOpenEx(path,GDAL_OF_VECTOR,nullptr,nullptr,nullptr);