# Invoice processing: Email to PostgreSQL database with OpenAI: n8n workflow

> Started by an arriving email, and works across Discord, Email and Postgres. It hands the judgement calls to OpenAI.

Source: https://millionrounds.com/workflow/invoice-processing-email-to-postgresql-database-with-openai (Millionrounds; every figure on the page links to its filing or article)

[Automation library](https://millionrounds.com/workflows) · [Money admin](https://millionrounds.com/workflows?category=Money+admin#library)

# Invoice processing: Email to PostgreSQL database with OpenAI

Started by an arriving email, and works across Discord, Email and Postgres. It hands the judgement calls to OpenAI. 23 steps, 5 branches: large, and worth reading before you trust it with anything live.

Large Calls a model: OpenAI

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Finvoice-processing-email-to-postgresql-database-with-openai)

**23 steps** Large: read it before trusting it with anything live

**5** Branches: places the flow splits

**0** Loops or waits

**3** Services it connects to

## The flow

Model 3 Logic 13 Service 7

The steps in the order they run, from what starts it.

- 1 **Email Trigger (IMAP)** Email Read Imap *Service*
- 2 **OpenAI Chat Model** Lm Chat Open Ai *Model*
- 3 **Structured Output Parser** Output Parser Structured *Model*
- 4 **If** If *Logic*
- 5 **Basic LLM Chain** Chain Llm *Model*
- 6 **Code** Code *Logic*
- 7 **Merge** Merge *Logic*
- 8 **If1** If *Logic*
- 9 **If2** If *Logic*
- 10 **Extract from File1** Extract From File *Service*
- 11 **Discord** Discord *Service*
- 12 **Prepare Company Data** Set *Logic*
- 13 **Check Company** Postgres *Service*
- 14 **IF Company Does Not Exist** If *Logic*
- 15 **Add Company** Postgres *Service*
- 16 **Set Existing Company ID** Set *Logic*
- 17 **Set New Company ID** Set *Logic*
- 18 **Merge Company ID** Merge *Logic*
- 19 **Check Invoice** Postgres *Service*
- 20 **IF Invoice Does Not Exist** If *Logic*
- 21 **Add Invoice** Postgres *Service*
- 22 **Invoice Exists** Set *Logic*
- 23 **Merge End** Merge *Logic*

## How it's built

- **Starts on:** An arriving email
- **Steps:** 23, with 5 branches
- **Size:** Large: read it before trusting it with anything live
- **Connects to:** Discord, Email, Postgres
- **Model:** OpenAI

## Questions

What does it need to run?

An n8n account, plus your own Discord, Email and Postgres accounts, and a key for OpenAI. You connect your own accounts after importing it; none are included.

What starts it?

It starts on an arriving email.

Does it use AI?

Yes. It calls OpenAI. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Take your time. At 23 steps it is large: read it before trusting it with anything live.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

**Before you run it**

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 23 steps before you point it at anything that matters.

## Run it in n8n

- **Create your n8n account** Free to start. n8n is where the workflow runs. [Create an n8n account ↗](https://millionrounds.com/n8n)
- **Download the workflow** Free with a Millionrounds account. [Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Finvoice-processing-email-to-postgresql-database-with-openai)
- **Paste it into n8n** Open a new workflow in n8n and paste. Then connect your own accounts.

Stored with the author's traces removed: canvas notes, instance identifiers, saved credential names and any live document they had left behind.

## Connects to

Each opens the other workflows that use it.

[Discord](https://millionrounds.com/workflows?service=Discord#library)[Email](https://millionrounds.com/workflows?service=Email#library)[Postgres](https://millionrounds.com/workflows?service=Postgres#library)

## Workflows like it

Also money admin, with the same services where possible, closest in size first.

- [**Create Zoho Books invoices from email attachments** 23 steps · calls a model · Email](https://millionrounds.com/workflow/create-zoho-books-invoices-from-email-attachments)
- [**Generate invoices and send AI-powered reminders with Jotform** 23 steps · calls a model · Email, n8n data table](https://millionrounds.com/workflow/generate-invoices-and-send-ai-powered-reminders-with-jotform)
- [**Invoice overdue reminder system - delivered with Airtable** 21 steps · Airtable, Email](https://millionrounds.com/workflow/invoice-overdue-reminder-system-delivered-with-airtable)
- [**Run bounded dunning for Lago payment failures** 25 steps · Email, n8n data table](https://millionrounds.com/workflow/run-bounded-dunning-for-lago-payment-failures)
- [**Validate incoming XRechnung invoices from IMAP and archive them** 20 steps · Email, n8n data table](https://millionrounds.com/workflow/validate-incoming-xrechnung-invoices-from-imap-and-archive-t)
- [**Convert legal timesheets to LEDES and PDF invoices with OpenAI** 27 steps · calls a model · Email, Excel](https://millionrounds.com/workflow/convert-legal-timesheets-to-ledes-and-pdf-invoices-with-open)
