I need read/write geometry object from PostGIS.

Is there C++ library to do this?

  • From the client or server side? PostGIS itself is open source, download the source from their website. postgis.net/install or you can use the OGR objects in C++ with the PostGIS driver. Commented May 3, 2016 at 3:42
  • @MichaelMiles-Stimson thx. I will try OGR. Do you know where I can find example code of writing PostGIS? I only find one reading example
    – max
    Commented May 3, 2016 at 4:05
  • 1
    You could start gdal.org/drv_pg.html about the driver and its special requirements.. I don't do pSQL/PostGIS directly myself but can attest that the tutorial gdal.org/ogr_apitut.html has some very useful gems.. one thing I will say though is to be sure to link 'additional dependencies' to the GDAL_i.lib - many hours of frustration over that one. Commented May 3, 2016 at 5:18

1 Answer 1


libpq allows us to connect to Postgres/postgis for read write operations. If you have installed Postgres,libpq is already available.It would be available at installation path like C:\Program Files\PostgreSQL\9.5\include .

//To make use in C/C++, you include these header files
#include "libpq/libpq-fs.h"
#include "libpq-fe.h"
#include <stdlib.h>

#include <string.h>
#include <conio.h>

//Declare some variables 

const char *conninfo;
PGconn     *conn;
PGresult *preslt;
//Connect to DB

conninfo = "dbname = lebl user=postgres password=abcd1234@";

/* Make a connection to the database */
conn = PQconnectdb(conninfo);

/* Check to see that the backend cfonnection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
    fprintf(stderr, "Connection to database failed: %s",
    printf("Connection Ok");
  • Good answer, though the OP does not mention Windows, specifically. Commented May 3, 2016 at 8:19
  • Thank you. I managed to insert geometry to postGIS with libpq. build the sql and call PQexec, amazing.
    – max
    Commented May 4, 2016 at 9:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.