Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Store doc, jpeg, pdf files in postgreSQL using java

In a web application I want to store all doc, jpeg, pdf files in postgreSQL database. I know it can also be done by storing it on file system and storing its path in db, but I am not looking for that solution. I want to know following things:

  1. What should be data type for a table.
  2. Does it maintain formatting of doc ?
  3. And how storing it in postgreSQL can be achieved using java, hibernate technology. IS it required to create nativeSQL query?

Any pointers for this issue will be helpful.

like image 267
devcodecc Avatar asked Jul 24 '12 05:07

devcodecc


People also ask

Can I store PDF files in PostgreSQL?

Probably the best way store PDF file in postgresql is via large object. You should have a table field of type OID . The create a large object with your PDF and then store the large object OID in the table.

Can we store documents in PostgreSQL?

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.

Can you store PDF files in a database?

PDF files are unstructured or semi-structured data, which means they don't have a fixed schema. This means that it can be challenging to store PDF file contents in a traditional SQL database. However, a NoSQL database is ideal for storing PDF file contents because it doesn't require a predefined schema.


1 Answers

You'll want to store the binary data as the bytea type. Here is an example using JDBC and Postgres bytea:

http://jdbc.postgresql.org/documentation/80/binary-data.html

This post about using bytea and Hibernate might be of some use: proper hibernate annotation for byte[]

like image 117
Cody Caughlan Avatar answered Sep 28 '22 05:09

Cody Caughlan