I want to insert bytes into my PostgreSQL (9.5.7) database column with the type bytea, using the Psycopg2 (2.7.1) copy_from() method.
I can insert my bytes with the following code :
psycopg2_cursor.copy_from(
StringIO("\x30\x40\x50"),
"my_table",
)
By executing a SELECT into my table after the insertion, I get the expected value from the bytea column:
\x304050
Now, I want to prepend my bytes with the byte 0:
psycopg2_cursor.copy_from(
StringIO("\x00\x30\x40\x50"),
"my_table",
)
I get the error : psycopg2.DataError: invalid byte sequence for encoding "UTF-8": 0x00. From my understanding, this error should only be triggered when inserting a null byte into a text field, but should work as expected into a bytea field. Am I missing something ? Is there any simple way to insert a null byte into a bytea column ?
Thanks!
standard_conforming_stringssetting?StringIO('\x30\x40\x50')instead?..