Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide whether to store binary data in Postgres or in files?

I'm developing a Postgres-backed system, in which many binary files will be stored.

I have at least 2 choices:

  1. Store them in Postgres.
  2. Store them as files.

What criteria do I need to consider to make the best possible decision?

like image 942
Dmitrii Pisarenko Avatar asked Feb 06 '16 10:02

Dmitrii Pisarenko


People also ask

Should binary files be stored in the database?

Do not store files in a database. Everyone, without exception, that can run any RDBMS on the market already has a database specifically for storing files, and the RDBMS itself is using it! That database is the filesystem.

Can Postgres store binary data?

PostgreSQL provides two distinct ways to store binary data. Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.

Why do we store data in binary?

Computer systems are designed to process data. In order to do so, computers use electronic circuits that function as switches that can be turned on or off. We use a binary form to represent these two states: A 0 represents when a switch is off.


1 Answers

I would consider the following:

  • Performance. Storing binary files in the file system generally performs better, both for reading and writing.
  • Security. Access to the files in the file system is controlled by the operating system, however if you store the files in the database, PostgreSQL access rules apply.
  • Backup consistence. If you store the data separately (file system and database) is very difficult to have a consistent backup. File system backup and database backup are likely to be out of sync.
  • Transactional properties. File system is not transactional, but obviously PostgreSQL is.
like image 190
Jesús López Avatar answered Oct 18 '22 22:10

Jesús López