How To Insert CSV to a Postgres Database in Mulesoft

In this tutorial, we are going to learn how to insert CSV files into a Postgres database using Mulesoft.

Prerequisites

  • A prerequisite to this is to download the JBDC driver for Postgres. We will refer to it later once we set up the connection.
  • A running Postgres database with the table configured. Check my video for installing Postgres locally on your Mac machine.

Building the App

This is the finished app we are going to build.

First, create a new Mulesoft app and add a Listener component then configure the connection.

Add a transformer to change the input from CSV to an array using dataweave.

Map the CSV Input:

Map the Output

Next, add a For Each component then Insert Database component. Configure the connection.

Under Required Libraries, click on Configure and choose Add Maven dependency

Then choose Install

Next, browse for the JDBC Postgres jar file and hit Install.

Next, configure the database to connect to your Postgres server either locally or remote.

My sample configuration.

URL: jdbc:postgresql://localhost:5432/csvdb

Driver class name: org.postgresql.Driver

User:

Password:

Test the connection and if successful.

Next, configure the SQL query and parameters then run the application.

Open postman or SOAP UI and test your URL endpoint. Make sure the application/csv is set for the content Type. Hit Send.

You should then see your records when you query your database.

You can also refer to my GitHub project for the README steps and code for this project.

Hope you like this simple tutorial, hit the comments below if you have questions.

https://jdbc.postgresql.org/download.html

Leave a Reply

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