I'm attempting to store binary data in a database. (postgresql on heroku)
I understand there are two different ways to store binary data in postgresql. A blob and a bytea..
When I create a table in my migration,
create_table :binaries do |t|
t.binary :data
end
it creates a column in the database of type bytea.
My question is.. How do I create a record of type blob?
Why do I ask? It seems when I send a ten byte file up to heroku, it stores it as a string of hex values, prepended with an "e".. so my 10 bytes becomes 21. My 10 meg file would become 20 megs (and one byte), ext, ext, ext...
Now that bothers me, but as I don't really care about performance. (I've had the care beaten out of me by the PM), its not what bothers me the most.
What really bothers me is; when I read out the contents of the database I get the 21 bytes, not the 10. That is un-useable.
So my question again.. How do I create a BLOB column in rails/postgresql/heroku environment?
BLOB stands for the binary large object that is used to store binary data such as the content of a file. PostgreSQL does not have the BLOB data type. However, you can use the bytea data type for storing the binary string. We will create a new table named company_files to store the binary string.
Standard SQL defines BLOB as the binary large object for storing binary data in the database. With the BLOB data type, you can store the content of a picture, a document, etc. into the table. PostgreSQL does not support BLOB but you can use the BYTEA data type for storing the binary data.
BLOB is the family of column type intended as high-capacity binary storage. The actual BLOB column type is of four types-TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB. The four BLOB types are very similar to each other; the only difference is the maximum amount of data each can store.
“Blob” stands for “binary large object” and refers to raw binary data stored in a database. Blobs can be images, audios, or other large file formats. Databases handle blobs differently, and here we will see an example of how an image is processed in PostgreSQL and PHP.
bytea
is PostgreSQL's version of a BLOB. From the fine manual:
The SQL standard defines a different binary string type, called
BLOB
orBINARY LARGE OBJECT
. The input format is different frombytea
, but the provided functions and operators are mostly the same.
So bytea
is what you want. As far as the format goes:
The
bytea
type supports two external formats for input and output: PostgreSQL's historical "escape" format, and "hex" format. Both of these are always accepted on input. The output format depends on the configuration parameter bytea_output; the default is hex. (Note that the hex format was introduced in PostgreSQL 9.0; earlier versions and some tools don't understand it.)
So what you're seeing is just the text versions that are used for getting data into the database and out of the database.
This might also be of interest:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With