Unable to convert PostgreSQL text column to bytea
NickName:powo Ask DateTime:2013-10-10T23:31:41

Unable to convert PostgreSQL text column to bytea

In my application I am using a postgresql database table with a "text" column to store pickled python objects. As database driver I'm using psycopg2 and until now I only passed python-strings (not unicode-objects) to the DB and retrieved strings from the DB. This basically worked fine until I recently decided to make String-handling the better/correct way and added the following construct to my DB-layer:

psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)

This basically works fine everywhere in my application and I'm using unicode-objects where possible now.

But for this special case with the text-column containing the pickled objects it makes troubles. I got it working in my test-system this way:

  • retrieving the data: SELECT data::bytea, params FROM mytable
  • writing the data: execute("UPDATE mytable SET data=%s", (psycopg2.Binary(cPickle.dumps(x)),) )

... but unfortunately I'm getting errors with the SELECT for some columns in the production-system:

psycopg2.DataError: invalid input syntax for type bytea

This error also happens when I try to run the query in the psql shell.

Basically I'm planning to convert the column from "text" to "bytea", but the error above also prevents me from doing this conversion.

As far as I can see, (when retrieving the column as pure python string) there are only characters with ord(c)<=127 in the string.

Copyright Notice:Content Author:「powo」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19300113/unable-to-convert-postgresql-text-column-to-bytea

More about “Unable to convert PostgreSQL text column to bytea” related questions

Unable to convert PostgreSQL text column to bytea

In my application I am using a postgresql database table with a "text" column to store pickled python objects. As database driver I'm using psycopg2 and until now I only passed python-strings (not

Show Detail

Unable to convert PostgreSQL text column to bytea

In my application I am using a postgresql database table with a "text" column to store pickled python objects. As database driver I'm using psycopg2 and until now I only passed python-strings (not

Show Detail

Get size for Bytea column in postgreSQL

I have table for store file information. File content is stored in column (column datatype is bytea in PostgreSQL). Now I want to get file content size, based on bytea column data. In SQL Server,...

Show Detail

How to convert PostgreSQL escape bytea to hex bytea?

I got the answer to check for one certain BOM in a PostgreSQL text column. What I really like to do would be to have something more general, i.e. something like select decode(replace(textColumn, '...

Show Detail

How to change the data type from bytea[] to bytea of column in PostgreSQL?

I am new to PostgreSQL. I want to change one of my column datatype from bytea[] to bytea in Postgres. I tried the following query but it is giving the error cannot cast type bytea[] to bytea: ALTER

Show Detail

How to convert bit type to bytea type in Postgresql

I want to convert bit type into bytea in postgresql. Like this. select (b'1010110011001100' &amp; b'1011000011110000')::bytea; However, error occured ERROR: cannot cast type bit to bytea LINE 1:

Show Detail

Equivalent of Convert varbinary in PostgreSQL for bytea

What is the equivalent of Convert varbinary in PostgreSQL for bytea? Here's a SQL Server example: CONVERT(varbinary(MAX),

Show Detail

SQL function to convert NUMERIC to BYTEA and BYTEA to NUMERIC

In PostgreSQL, how can I convert a NUMERIC value to a BYTEA value? And BYTEA to NUMERIC? Using TEXT values I can use CONVERT_TO() and CONVERT_FROM(). Is there anything simmilar? If not, how would i...

Show Detail

Read PostgreSQL Bytea column in Ruby

I am trying to read a bytea column from PostgreSQL using Ruby. The problem is that it returns the hex encoded string of the bytea value. I would like it to return exactly the same as if I would ope...

Show Detail

PostgreSQL - How to convert current timestamp into bytea

I have migrated a database from SQL Server into PostgreSQL. A number of tables contain a ts column(timestamp) which stores the rowversion and is autogenerated in SQL server. When I converted the ...

Show Detail