# Rails and PostgreSQL bytea

**URL:** <https://www.ruby-forum.com/t/rails-and-postgresql-bytea/52719>\
**Category:** Rails\
**Created:** [January 18, 2006, 11:11am UTC](https://www.ruby-forum.com/t/rails-and-postgresql-bytea/52719 "2006-01-18T11:11:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickthemick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@rickthemick](https://www.ruby-forum.com/u/rickthemick)\
**Post date:** [January 18, 2006, 11:11am UTC](https://www.ruby-forum.com/t/rails-and-postgresql-bytea/52719/1 "2006-01-18T11:11:08Z")

</div>

Hi!  
I’m trying to insert binary data into a PostgreSQL database where the  
field is of type ‘bytea’. This works fine if a do Base64 encoding but I  
just want to do character escaping. How do I do this???.

What I get is this:  
ActiveRecord::StatementInvalid in Init\_test#save\_file

ArgumentError: Invalid Ruby/cstring: INSERT INTO  
globalfiles(chksum,block,directory,filename,blocksize,time,data)  
VALUES(‘10fcfa244eb972c6de695be235d11f98’, ‘0’, ‘test’, ‘myfilename’,  
‘500000’, ‘NOW()’, ‘ï¿½Bï¿½ï¿½Hï¿½3ï¿½!g1hVï¿½ï¿½9 blaaaahh more binary data…’

I have got a indication that this should be done automatically, but does  
this hold for the postgres-pr module?

Best Regards  
Rickard

Btw, I’m using Ruby 1.8.2, postgres-pr 0.4.0 and Rails 1.0.0

---

<div class="post-metadata">

**Author:** ![rickthemick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@rickthemick](https://www.ruby-forum.com/u/rickthemick)\
**Post date:** [January 19, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/rails-and-postgresql-bytea/52719/2 "2006-01-19T18:22:53Z")

</div>

Hi again!  
I would really appreciate input from you guys who know this or have  
tried it.

Any one having ideas?

My problem is also described here:

> **[8.4. Binary Data Types](https://www.postgresql.org/docs/7.4/datatype-binary.html)**
>
> 8.4. Binary Data Types 8.4.1. bytea Hex Format 8.4.2. bytea Escape Format The bytea data type allows storage of binary strings; …

I have read and tried, I just can get it to run properly. I always get  
the error described below. I hope there would be some nice “the Rails  
way…”

Many thanx in advance!

Best Regards  
Rickard

rickthemick wrote:

> Hi!  
> I’m trying to insert binary data into a PostgreSQL database where the  
> field is of type ‘bytea’. This works fine if a do Base64 encoding but I  
> just want to do character escaping. How do I do this???.
> 
> When I just to a plain insert as if it were ASCII data I get:  
> ActiveRecord::StatementInvalid in Init\_test#save\_file
> 
> ArgumentError: Invalid Ruby/cstring: INSERT INTO  
> globalfiles(chksum,block,directory,filename,blocksize,time,data)  
> VALUES(‘10fcfa244eb972c6de695be235d11f98’, ‘0’, ‘test’, ‘myfilename’,  
> ‘500000’, ‘NOW()’, ‘ï¿½Bï¿½ï¿½Hï¿½3ï¿½!g1hVï¿½ï¿½9 blaaaahh more binary data…’
> 
> I have got a indication that this should be done automatically, but does  
> this hold for the postgres-pr module?
> 
> Best Regards  
> Rickard
> 
> Btw, I’m using Ruby 1.8.2, postgres-pr 0.4.0 and Rails 1.0.0
