I want to create a repository system where people can store videos, audios, pdfs, images, etc. I will probably use PGSQL and wonder what the best strategy for storing this data is that it may be different in size and type.
Thanks in advance.
I want to create a repository system where people can store videos, audios, pdfs, images, etc. I will probably use PGSQL and wonder what the best strategy for storing this data is that it may be different in size and type.
Thanks in advance.
The simplest solution is to store the file in a directory (folder) and the location (or other reference to this file) in the database. For example, let's say you go to store PDF files. You can save the documents in a directory called "PDFs" and create a table with id and location of these documents. As in id: 0001, uri: PDFs / documento_0001.pdf
You could use software such as DSpace , for example.
You can store the contents of a file in a column of type BYTEA
let's see:
CREATE TABLE tb_arquivo
(
id BIGSERIAL PRIMARY KEY, -- Identificador unico do arquivo
nome TEXT NOT NULL, -- Nome/Path original do arquivo
conteudo BYTEA NOT NULL, -- Conteudo do arquivo
tamanho BIGINT NOT NULL, -- Tamanho total do arquivo
md5 TEXT NOT NULL -- Assinatura MD5 do arquivo
);
The content of the file can be read through the function pg_read_binary_file()
and its attributes can be obtained through the function pg_stat_file()
.
Assuming the file is written to the database server disk in your PG_DATA
:
Writing a file to this table would look like this:
INSERT INTO
tb_arquivo ( nome, conteudo, tamanho, md5 )
VALUES
( 'imagem.png',
pg_read_binary_file('imagem.png'),
(pg_stat_file('imagem.png')).size,
md5(pg_read_binary_file('imagem.png')) );