How to insert a binary file and query it in Postgres?

Describe the problem/error/question

I have a webhook node that brings in a set of office document files (typically .xlsx or .doc). I need to store them in a Postgres Database for further processing. How to send a binary file to a Postgres row?

(I have created the table with bytea data type. If I need to modify it, pls mention)

Also, when we Select (query) the rows from the DB, will we get the binary file too?

Please share your workflow

Information on your n8n setup

  • n8n version: 0.230.3
  • Database: Postgres
  • n8n EXECUTIONS_PROCESS setting: own
  • Running n8n via: Docker
  • Operating system: Ubuntu Server 22.04 LTS

Hi @Jayavel, the exact approach will of course depend on how your incoming data looks like, but assuming you are having a binary item named data coming from your webhook something like below would let you insert a binary file into a bytea column.

Table schema

                                        Table "public.uploads"
   Column   |            Type             | Collation | Nullable |               Default               
 id         | integer                     |           | not null | nextval('uploads_id_seq'::regclass)
 created_at | timestamp without time zone |           | not null | now()
 data       | bytea                       |           |          | 
    "uploads_pkey" PRIMARY KEY, btree (id)


This example uses the Move Binary Data node to create a base64 string based on the binary data:

It then uses a custom query for the INSERT step, relying on the decode function provided by PostgreSQL.

You can also read the data the other way around, using the encode function (and then the Move Binary data node to re-create your binary item in n8n):

I’ve used an image file here to illustrate the function, but of course this will work for other file types too. When processing different file types you probably want to keep a record of the filename as well as the MIME type though.

Hope this helps!


Hey @MutedJam,

Thanks for providing a detailed explanation. I’ve implemented this and got my workflow up and running.

Thank you!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.