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

**URL:** <https://community.n8n.io/t/how-to-insert-a-binary-file-and-query-it-in-postgres/27236>\
**Category:** Questions\
**Tags:** postgres\
**Created:** [June 17, 2023, 7:09am UTC](https://community.n8n.io/t/how-to-insert-a-binary-file-and-query-it-in-postgres/27236 "2023-06-17T07:09:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jayavel](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jayavel](https://community.n8n.io/u/Jayavel)\
**Post date:** [June 17, 2023, 7:09am UTC](https://community.n8n.io/t/how-to-insert-a-binary-file-and-query-it-in-postgres/27236/1 "2023-06-17T07:09:01Z")

</div>

### 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

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [June 19, 2023, 1:55pm UTC](https://community.n8n.io/t/how-to-insert-a-binary-file-and-query-it-in-postgres/27236/3 "2023-06-19T13:55:09Z")

</div>

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

```auto
                                        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 | | | 
Indexes:
    "uploads_pkey" PRIMARY KEY, btree (id)

```

# Workflow

This example uses the [Move Binary Data](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.movebinarydata/) node to create a base64 string based on the binary data:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/0/b/0b351a9a4bff6b94445072ab31e411fc43e0abf3.png)

It then uses a custom query for the INSERT step, relying on the [decode function](https://www.postgresql.org/docs/15/functions-binarystring.html#FUNCTIONS-BINARYSTRING-CONVERSIONS) 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):

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/8/e/8eaa8e11d506c13326bae1b4e0377b5e2de81697.jpeg)

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!

---

<div class="post-metadata">

**Author:** ![Jayavel](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jayavel](https://community.n8n.io/u/Jayavel)\
**Post date:** [June 20, 2023, 4:35am UTC](https://community.n8n.io/t/how-to-insert-a-binary-file-and-query-it-in-postgres/27236/4 "2023-06-20T04:35:59Z")

</div>

Hey @MutedJam,

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

Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [June 27, 2023, 4:36am UTC](https://community.n8n.io/t/how-to-insert-a-binary-file-and-query-it-in-postgres/27236/5 "2023-06-27T04:36:11Z")

</div>

Closed on June 27, 2023, 4:36am UTC

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