Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting text string with hex into PostgreSQL as a bytea

Tags:

sql

postgresql

I have a text file with several strings of hex in it:

013d7d16d7ad4fefb61bd95b765c8ceb 007687fc64b746569616414b78c81ef1 

I would like to store these in the database as a bytea, instead of a varchar. That is, I would like the database to store 01 as the single byte 00000001, not characters '0' & '1'.

I can easily run this file through sed to format/escape it any way I need to.

This is what I have tried:

create table mytable (testcol BYTEA); 

This works:

insert into mytable (testcol) values (E'\x7f\x7f'); 

However, as soon as I have a byte that goes above \x7f, I get this error:

insert into mytable (testcol) values (E'\x7f\x80'); ERROR:  invalid byte sequence for encoding "UTF8": 0x80 

Any ideas, or am I approaching things wrong?

like image 578
Donald Miner Avatar asked Jun 23 '10 15:06

Donald Miner


People also ask

What is Bytea datatype in PostgreSQL?

The bytea data type allows the storage of binary strings or what is typically thought of as “raw bytes”. Materialize supports both the typical formats for input and output: the hex format and the historical PostgreSQL escape format. The hex format is preferred.

What is blob in PostgreSQL?

Blob (Binary large object) is an Oracle data type that is used to store binary data like contents of a file or information like audio, video, and images. PostgreSQL does not have the Blob data type directly, but we can work with it using the methods below.

Can we store blob in PostgreSQL?

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. We will store the content of a file in the file_data column.

What is decode in PostgreSQL?

PostgreSQL DECODE() function is used to decode or extract the binary data from the input string, which is in textual format and which has been encoded by using PostgreSQL Encode() function.


2 Answers

You can convert a hex string to bytea using the decode function (where "encoding" means encoding a binary value to some textual value). For example:

select decode('DEADBEEF', 'hex');       decode       ------------------  \336\255\276\357 

which is more understandable with 9.0's default output:

   decode    ------------  \xdeadbeef 

The reason you can't just say E'\xDE\xAD\xBE\xEF' is that this is intended to make a text value, not a bytea, so Postgresql will try to convert it from the client encoding to the database encoding. You could write the bytea escape format like that, but you need to double the backslashes: E'\\336\\255\\276\\357'::bytea. I think you can see why the bytea format is being changed.... IMHO the decode() function is a reasonable way of writing inputs, even though there is some overhead involved.

like image 96
araqnid Avatar answered Sep 26 '22 09:09

araqnid


 INSERT INTO   mytable (testcol) VALUES   (decode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex')) 
like image 21
Julius Musseau Avatar answered Sep 26 '22 09:09

Julius Musseau