Postgres Database Support as Main Datastore

This page is for documenting PostgreSQL Database Support as the Main Data store.

Child Pages


OpenSRP supports using Postgres as the primary datastore(database) for storing OpenSRP data. OpenSRP supports using either CouchDb or Postgres as the primary database; only one can be enabled at a time.

In this documentation data access objects (DAO) as referred as repository objects/beans.

The classes that handle the database logic (repositories) as defined as interfaces. There are Postgres and CouchDb implementations of the repository interface to handle custom logic for each database. This allows OpenSRP to support using any of the two databases.

OpenSRP uses Spring framework component scan to initialize and load beans. So the database classes(repositories) are loaded depending on the configuration file loaded 

In the global OpenSRP spring configuration file applicationContext-opensrp.xml one can either import/load

  1. persistence_couchdb.xml
  2. persistence_postgres.xml

The first file enables CouchDb as the primary database while importing the second file enables Postgres as the primary database.

OpenSRP does not support use both database as runtime. So only one database type should be used.

Each of the configuration files above enables component scan for OpenSRP base package "org.opensrp" while excluding the unrequired database specific beans.
For example if Postgres is imported CouchDb beans are not loaded in the spring context and are not candidate for dependency injection leaving only Postgres beans as the only candidates for dependency injection.

Note: The form submission model was not migrated to use Postgres.  Anyone using form submission model must first implement the Postgres repositories beans before migrating to Postgres


Below is an example of applicationContext-opensrp.xml with Postgres enabled

Maven profiles have been configured on the pom to enable building OpenSRP to either Postgres  or CoachDB as the main database. Postgres is the default if no profile is specified.

One can build by specifying the maven profile flag(-P <Profile>) or by passing an argument (opensrp.database.type) to maven with the database type

To build with Postgres use 

mvn clean package  -Dopensrp.database.type=postgres or 

mvn clean package -P postgres

To build with CouchDB

mvn clean package  -Dopensrp.database.type=couchdb Or

mvn clean package -P couchdb