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 contents 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 disk of the database server in its directory PG_DATA
:
Recording a file in this table would be something like:
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')) );
I believe that the best way for you to do this is by saving the path of the files in the bank, and saving the files themselves on a page on your server
– Leonardo Theodoro