Upload csv file and create records in PostGresSQL


I am new to Retool and PostgreSQL.
I am doing a POC to create input form in Retool for user to upload an csv file. then I need to parse it and create new records in PostGresSQL table. I am looking for the sample for following:

  1. How to create input form with file upload dialog?
  2. Code to parse and validate csv
  3. Write to PostGresSQL


Hey Parthib - welcome to the forum!

There are few templates available on the following links.
Import CSV to PostgreSQL (retool.com)
Insert CSV data into a database (retool.com)

Let me know if you have any questions!

Hi Stefan

I tried the first option Import CSV to PostgreSQL (retool.com) as this method is relevant to my requirements.

But when I try, I am getting error records.map is not a function, how to fix this error?

Hey there could you share how the query is set up and records so we can pinpoint what is causing the issue.

on the insertData query, i choose 'managed_db' as resource, GUI Mode, "Run Query only when manually triggered',

Table: Employee
Action Type: Bulk Insert Records

So depending where you are pulling records from it needs to have .data or .value (if from transformer) behind it to work. So if you do records.data.map(x=>x) as an example it should work as long as the data itself is array.

Hope that helps!

What options i should select in 'insertData' query?. Because I am uploading different csv file than 'Employee' table. So, how this query works?. should I create my csv's table columns first then select this table in the query?.

In order for the upload to work you need data and type of it match the database table.

So if your CSV i.e. contains id (int),name(string),count(int) your selected table in "inputData" has to have those three columns with the matching type/rules.

1 Like

Thank you. It is working now after adding below snippet: