How do you create a BLOB column in rails postgresql database
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:
Blob storage is built into Postgres and is supported using the ActiveRecord adapter, but only directly through the raw connection and the lo_*
methods. You can use lo_write, lo_open, lo_close and lo_read
to create and manipulate blobs. Creating a blob returns an OID which you can reference the blob in your models.
You can add this using a migration
rails g migration AddFileToModel file:oid
Or directly like this
add_column :users, :avatar, :oid
For a working example of this you should look at the Carrierwave PostgreSQL gem. You can either build a custom solution based on that code, or just use Carrierwave directly. I'm currently using it and it works well.