如何将视频存储在 PostgreSQL 数据库中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8970636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store videos in a PostgreSQL database?
提问by sat
I am storing image files(like jpg
, png
) in a PostgreSQL database. I found information on how to do that here.
我将图像文件(如jpg
, png
)存储在 PostgreSQL 数据库中。我在此处找到了有关如何执行此操作的信息。
Likewise, I want to store videosin a PostgreSQL database. I searched the net - some say one should use a data type such as bytea
to store binary data.
同样,我想将视频存储在 PostgreSQL 数据库中。我在网上搜索 - 有人说应该使用一种数据类型bytea
来存储二进制数据。
Can you tell me how to use a bytea
column to store videos?
你能告诉我如何使用bytea
列来存储视频吗?
回答by Erwin Brandstetter
I would generally notrecommend to store huge blobs (binary large objects) inside PostgreSQL if referential integrity is not your paramount requirement. Storing huge files in the filesystem is much more efficient:
Much faster, less disk space used, easier backups.
如果参照完整性不是您的首要要求,我通常不建议在 PostgreSQL 中存储巨大的 blob(二进制大对象)。在文件系统中存储大文件效率更高:
速度更快,使用的磁盘空间更少,备份更容易。
I have written a more comprehensive assessment of the options you've got in a previous answerto a similar question. (With deep links to the manual.)
回答by Pavel Stehule
We did some tests about practical limits of bytea datatype. There are theoretical limit 1GB. But practical limit is about 20MB. Processing larger bytea data eats too much RAM and encoding and decoding takes some time too. Personally I don't think so storing videos is good idea, but if you need it, then use a large objects - blobs.
我们对 bytea 数据类型的实际限制做了一些测试。有理论限制1GB。但实际限制约为 20MB。处理更大的字节数据会占用太多内存,编码和解码也需要一些时间。就我个人而言,我认为存储视频不是个好主意,但如果您需要它,请使用大对象 - blob。
回答by zgpmax
Without knowing what programming language you are using, I can only give a general approach:
在不知道你使用的是什么编程语言的情况下,我只能给出一个通用的方法:
- Create a table with a column of type 'bytea'.
- Get the contents of the video file into a variable.
- Insert a row into that table with that variable as the data for the bytea column.
- 创建一个包含类型为“bytea”的列的表。
- 获取视频文件的内容到一个变量中。
- 使用该变量在该表中插入一行作为 bytea 列的数据。