Applications often need to store files.
Examples include:
- Profile images
- PDFs
- Product photos
- Videos
- Documents
- Chat attachments
The question is:
Should the file go into the database or object storage?
For most application files, object storage is the more practical choice.
What Should Go in PostgreSQL?
PostgreSQL is excellent for structured information.
For a file, your database might store:
id
user_id
filename
storage_path
file_type
created_at
The actual file can live in storage.
What Should Go in Storage?
Large binary files are generally better suited to object storage.
Examples:
image.jpg
document.pdf
video.mp4
invoice.pdf
The application stores a reference to the file in PostgreSQL.
A Simple Architecture
PostgreSQL
|
+-- file metadata
|
+-- storage path
|
v
Supabase Storage
Why Not Store Everything in the Database?
Large binary data can increase database size and complicate database operations.
Keeping files in object storage separates:
Structured data
from
File objects
This makes the architecture easier to manage.
Public vs Private Files
Not every file should be public.
Public examples:
- Product images
- Public blog images
- Marketing assets
Private examples:
- User documents
- Invoices
- Identity-related files
- Internal company documents
Private files should use appropriate access controls.
Signed URLs
For private files, your application can generate temporary access URLs when appropriate.
This allows users to access a file without making the storage object permanently public.
File Metadata
Your database can track:
owner
filename
size
type
path
upload_date
This makes it easier to search and manage files.
File Deletion
When deleting a database record, remember that the corresponding storage object may also need to be removed.
Otherwise, unused files can accumulate.
Backups
Your database backup strategy and file-storage backup strategy should both be considered.
Backing up database metadata does not automatically mean the original file objects are protected in the same way.
Supabase Storage
Supabase includes object storage as part of its backend platform.
This allows an application to keep database records and file storage within the same broader backend architecture.
Final Thoughts
A simple rule works for most applications:
Database for structured metadata. Storage for large files.
Keep the connection between them using IDs and storage paths.
This approach keeps your Supabase architecture cleaner as your application grows.