r/n8n • u/easybits_ai • 3d ago
Servers, Hosting, & Tech Stuff Storing generated PDFs in Postgres vs object storage: where do you draw the line?
👋 Hey n8n Community,
Follow-up on the approval system I posted about last week. One decision I made this week that I am still not sure about.
The workflow generates a final PDF once Finance approves a request. People download it from a portal afterwards, sometimes repeatedly, sometimes months later. Regenerating on every download meant paying for the conversion again and making them wait several seconds each time.
So I cache it: the PDF is generated at the moment of approval and stored as base64 in a column on the fact sheets table, then served from there.
What I think I got right
Generating at approval rather than on demand means the document always carries the real approval timestamps, and I never pay twice for the same conversion. Caching any earlier would have meant storing drafts that never got approved.
What I am less sure about
Base64 in Postgres is simple. Everything lives in one place, no extra service, no signed URLs, no bucket permissions. But it is blobs in a relational database, and I have not yet hit the point where row sizes start to hurt. The alternative is object storage with a reference in the row, which is more correct and also more moving parts for something a handful of people download a few times a month.
Where would you draw that line? Curious whether people go to object storage from day one or wait until something actually breaks.
Best,
Felix
3
u/Low_Rush_8535 2d ago
i'd draw the line at restore time. seed staging with a year's worth of PDFs at your actual file sizes, then time a full restore and check that an old approved PDF still matches byte for byte. if that fits your recovery window, i'd keep them where they are.
3
u/Weekly-Werewolf4007 2d ago
For a small workflow, I'd keep the simple setup and store the PDF as bytea instead of base64, assuming the n8n step can pass binary data. I'd watch how much the files add to backup size and restore time. Those are the numbers I'd use to decide when a bucket is worth it.
3
u/GFAutoHQ 2d ago
If you stay in Postgres, switch the column from base64 text to bytea. Base64 makes each file about a third bigger, so bytea saves that space for free. Also keep the PDF column out of any query that lists rows, and only pull it in the download step, so normal lookups stay fast. The thing that usually pushes people to object storage is backups, since every PDF rides along in every database dump.
8
u/Free-Good9203 2d ago
Everyone already covered bytea vs base64 and backup/restore times, so one small schema tweak if you decide to stay in Postgres for now: put the PDF blob in its own dedicated child table (approval_id FK, pdf_bytes bytea, sha256 text) instead of a column on fact_sheets.
It prevents any future SELECT * from accidentally dragging binaries into memory, keeps vacuum and row bloat off your hot operational table, gives you a hash to verify the approved document was never altered, and makes migrating to S3/R2 later a clean one-table swap without touching the core schema.
3
u/tankerkiller125real 2d ago
As an IT guy it comes down to this.
Is it a file of any kind? Yes? It goes in a proper blob storage system/file store. Full stop, no exceptions, no "Oh it's only a few of them" no BS.
I've been bitten in the ass way too many times by developers and engineers that insisted that they would at max be storing only a few MB at most of blob objects in SQL, only for it to turn into 100s of GBs down the line when they decided "Hey, we did it this way last time, might as well do it again for this entirely different thing".
2
u/runtime-ghost 2d ago
Storing base64 in Postgres works at tiny scale because it avoids managing bucket IAM and signed URLs, whereas it quietly adds about 33 percent storage overhead, bloats TOAST tables, and slows down WAL replication and pg_dump restores as volume grows. As a developer, I draw the line using query shape and total retention size rather than an arbitrary file count: if total PDFs stay under a few gigabytes and you only select the blob column by primary key on download (never in list queries), switching from base64 to binary bytea keeps everything atomic in one database, whereas once backups stretch your recovery window or n8n workers hit memory limits passing blobs around, write the file to S3 or Cloudflare R2 and store only the object key and SHA-256 hash in Postgres.
1
u/Aide_Sea 2d ago
One thing nobody mentioned: serving it from Postgres means every download holds a DB connection while the file streams out. Fine for a few people, but I think that's maybe the first thing that'd hurt, more than row size :)
1
u/Electronic_Special48 2d ago
A tip: if the PDFs you are storing are not special (no digital signatures, no high-res diagrams, ...), you can optimize/compress the PDFs before storing them.
Using ghostscript, something like this works wonders:
1
u/RabHanz 1d ago
Generating at approval is the right call. On where to store it, at a handful of downloads a month Postgres is fine, but I'd change two things now and set one trigger for moving.
The trigger I use for moving: when the database backup starts taking noticeably longer because of the files, or once the files are most of the database's size. Backups and restores are where blobs in Postgres actually hurt, long before query speed does.
When I've moved files out (to S3-compatible storage, MinIO when self-hosted), the row keeps the key and the hash, and downloads go through a short-lived signed URL. That's maybe an hour of work, so I wouldn't do it on day one for your volume.
1
u/lakeland_nz 1d ago
What benefit are you getting from storing them in the DB?
I get that it's possible, but why is it desirable?
1
u/FlowsmithLabs 22h ago
Generating at approval is the right call, for exactly the reason you gave.
On storage, a few things that might move the line for you:
bytea**, not base64 text.** Base64 adds about a third to the size.byteastores the raw bytes, and Postgres moves large values out of the main table (TOAST) on its own.