A
A
Andrey Astafiev2016-11-16 13:53:32
PostgreSQL
Andrey Astafiev, 2016-11-16 13:53:32

How to save a file in Postgresql database?

How to save a file (*.doc,*.png ...) to the database. What type should the cell have and the Sql command itself to save to the database

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2016-11-16
@Astafiev_Andrey

If the file is small, then you can stuff it into a field like bytea

create table images(id int, image bytea);
insert into images values (1, pg_read_file('/path/to/image.jpg')::bytea);

But keep in mind that during operations with the image field , PostgreSQL will load its contents into memory. So it's better to use BLOB
create table images (id int, image oid); 
insert into images values (1, lo_import('/path/to/image.jpg'));

N
nApoBo3, 2016-11-16
@nApoBo3

It is better not to store files in the database at all. Depending on the project, the path to the file can be various relative or absolute links, or data for generating them.

R
romy4, 2016-11-16
@romy4

it's better to save the path to the file
and always store the file on the hard disk
. How do you imagine the return of this file in the browser?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question