Best Practices for Removing All Data or Recreating DHIS2 Database

Hi everyone,

I’m working with a DHIS2 instance 2.40, using the standalone installation from the system administrator guide and need to completely remove all existing data and start fresh. I’m considering recreating the database from scratch using my standalone application.

Could you please advise on the best way to achieve this?

Specifically, I’m interested in:

  • Recommended methods for removing all data: Should I truncate database tables, or drop and recreate the entire database?
  • Are there any specific steps or configurations I should follow to ensure a clean and successful database recreation?

Any guidance or insights from the community would be greatly appreciated.

Thanks in advance!

Hi @bencapriata

The best way will be following

  1. Export your metadata
  2. Create an empty database
  3. Point dhis.conf to that database
  4. Import the metadata

In case if you need some tracker data, create a list of data what you want to export to the new instance.

1 Like

Hey @Ulanbek, thanks for this. I just wanted to create a reply with the steps that worked for us for future reference.

Based on the DHIS2 system administrator guide;

Part 1: Creating an Empty Database

  1. First, create a non-privileged database user called ‘dhis’ by executing this command:
sudo -u postgres createuser -SDRP dhis

You’ll be prompted to enter a secure password for this user.

  1. Next, create the actual database with the following command:
sudo -u postgres createdb -O dhis dhis2

This creates a database named ‘dhis2’ owned by the ‘dhis’ user we just created.

  1. Since DHIS2 requires PostGIS extension for GIS/mapping features, you need to create it with:
sudo -u postgres psql -c "create extension postgis;" dhis2
  1. For DHIS2 version 2.38 and later, you also need to create two additional extensions:
sudo -u postgres psql -c "create extension btree_gin;" dhis2
sudo -u postgres psql -c "create extension pg_trgm;" dhis2

Part 2: Configuring dhis.conf

The guide specifies that you need to create a dhis.conf file in your DHIS2_HOME directory. Here’s how:

  1. Create a configuration directory (the guide suggests this location):
mkdir /home/dhis/config
chown dhis:dhis /home/dhis/config
  1. Create and edit the dhis.conf file in this location:
/home/dhis/config/dhis.conf
  1. Add these required database connection properties to the file:
# Database connection configuration
connection.dialect = org.hibernate.dialect.PostgreSQLDialect
connection.driver_class = org.postgresql.Driver
connection.url = jdbc:postgresql:dhis2
connection.username = dhis
connection.password = YOUR_PASSWORD_HERE
  1. Protect the configuration file since it contains sensitive information:
chmod 600 dhis.conf

After completing these steps, you’ll have an empty database created and properly configured for DHIS2 to use.

2 Likes

Thank you so much for being helpful and sharing this with the community. :pray: