Table of Contents

TerraLib Developer's Guid - Data Access → Drivers → PostGIS

The PostGIS native data access driver allows applications to access data stored in a PostgreSQL system with the the geo-spatial extension PostGIS enabled.

This section describes the design and implementation issues of this driver and how it can be used by applications.

Design

Creating a PostgreSQL Database

The PostGIS driver allows applications to create new PostgreSQL databases or drop an existing one.

In order to create a new database you must specify the connection parameters to an auxiliary database. This database will be used to stablish a temporary connection. All the connection info in the previous section can be used. Besides that information you must specify some parameters for the creation of the new database:

If you intend to connect to the database after its creation with different parameters from the auxiliary database, you must specify new parameters with the prefix: PG_NEWDB_. For instance, if you want a different user to be used in the new data source, you can specify: PG_NEWDB_USER = different-user-name. All parameters showned in the Connection Parameters section can be prefixed.

The code snippet below shows how to create a new POSTGIS database:

std::map<std::string, std::string> connInfo;
 
// auxiliary database connection parameters
connInfo["PG_HOST"] = "localhost";
connInfo["PG_USER"] = "postgres";
connInfo["PG_PASSWORD"] = "secret";
connInfo["PG_DB_NAME"] = "postgres";
 
// new database parameters
connInfo["PG_NEWDB_NAME"] = "terralib";
connInfo["PG_NEWDB_TEMPLATE"] = "template1";
 
// create new database
std::unique_ptr<te::da::DataSource> pgis(te::da::DataSource::create("POSTGIS", connInfo));
 
// the new datasource will in a close state
pgis->open();
 
// ...

Dropping a PostgreSQL Database

To drop a database you must specify the connection parameters to an auxiliary database. This database will be used to stablish a temporary connection. All the connection info in the previous section can be used. Besides that information you must specify:

The code snippet below shows how to drop an existing PostgreSQL database:

 

Connecting to a PostgreSQL Database