How to configure multi Postgres DataSources with Springboot

In the tutorial, JavaSampleApproach will guide you how to configure multi Postgres DataSources with Spring Boot.

I. Technologies for Multi Postgres Datasources tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview
1. Project Structure

springboot configure multi postgres datasources

Dao Layer: is an interface and is an implementation of
DataSource: Configured in used to define properties of data sources.

2. Step to do

– Create SpringBoot project.
– Setup Postgres Databases.
– Configure Datasource properties in file.
– Create a Model class.
– Build Dao Layer.
– Build a Client.

III. Practice
1. Create SpringBoot project.

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project’s info.
Press Next, then press Finish, a Spring Boot project will be created.

Open pom.xml file, add JDBC & PostgreSQL dependencies:

2. Setup Postgres Databases.

Create 2 PostgreSQL databases: db1 & db2
In each database, create a customer table with sql script:

3. Configure Datasource properties in file

Open, configure as below:
– Datasource properties for PostgreSQL db1

– Datasource properties for PostgreSQL db2:

4. Create a Model class.

5. Build Dao Layer.

– Configure DataSource Beans:

– Create interface

– Implement Customer Dao:
For db1 database, create

For db2 database, create

6. Build a Client.

IV. Sourcecode


By JavaSampleApproach | January 5, 2017.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *