Storing mail attachments in n8n/ postgres and retrieving file link

Describe the problem/error/question

i have built a knowledge base for outlook emails. The workflow adds selected mail field and processed attachments into pgvector. I also have a query workflow. Now, when i ask something, the ai agent answers and add source also. (filename, mail link)
But I want to store the file data, so that it is clickable and user can view or download it.

For now, I am using same database, but a different table for storing the binary attachment file.
In the table, I have mail id, file name, file data.

Now, via postgres node, I can insert into the table. But the problem is, {{ $binary.attachment }} only gives the file reference. It seems n8n uses file storage v2, So, with this setup, I can’t insert actual file, rather a reference!!

How to store the actual binary file?

and while inserting, do I need to maintain relationship or meta data, so that while querying, with each file name, file data is also attached.. Currently in vectorstore node, i am adding file name and mail id as meta data


Hey @sakibul hope all is well.

What I would suggest is - keep the files in a separate file database. Upon receiving the email download the attachments and upload them to a storage (s3, google drive,… whatever works for you). Get the link back. Ingest the email content and provide the link in metadata. On retrieving side, make sure to get the metadata and instruct the LLM to provide the link(s) to the attached files in the answer.

Show me what you have already, I will help you figure it out.

Attach your workflow so I can take a look.

Ok, you didn’t attach the workflow, but here, take a look at these examples:

write binary to postgres:

read binary from postgres:

And here is the database record:

image

This should give you enough context to be able to store and retrieve binaries to and from the database.

1 Like

Let me know if that helped.