# Escape bytea binary data in postgres SQL

**URL:** <https://www.ruby-forum.com/t/escape-bytea-binary-data-in-postgres-sql/50802>\
**Category:** Rails\
**Created:** [December 21, 2005, 10:38am UTC](https://www.ruby-forum.com/t/escape-bytea-binary-data-in-postgres-sql/50802 "2005-12-21T10:38:07Z")\
**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:** [December 21, 2005, 10:38am UTC](https://www.ruby-forum.com/t/escape-bytea-binary-data-in-postgres-sql/50802/1 "2005-12-21T10:38:07Z")

</div>

Hi!  
I want to escape binary data correctly either manually or using some  
pre-defined function that does this for me!

I have found on the web: PGconn.escape\_bytea but I only get “undefined  
method”. Any ideas, what is the correct way of doing this my self?

I’m using Rails (WEBrick) under Windows XP with postgre-pr 0.4.0

I have tried:

s\_buffer = s\_buffer.gsub(/[’]/, ‘\047’)  
s\_buffer = s\_buffer.gsub(/[\]/, ‘\134’)  
s\_buffer = s\_buffer.gsub(/[\0]/, ‘\000’)

/Rick

---

<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:** [December 21, 2005, 4:10pm UTC](https://www.ruby-forum.com/t/escape-bytea-binary-data-in-postgres-sql/50802/2 "2005-12-21T16:10:17Z")

</div>

The ActiveRecord does this automatically, as necessary.

Or, I’ve been _very_ lucky storing uploaded photos in a  
BYTEA in PostgreSQL.

–  
– Tom M.
