Monday, February 28, 2011

Converting MySQL blob data to PostgreSQL bytea

I just went through several annoying iterations of preparing to load a MySQL data set into a PostgreSQL server, with somewhat less than helpful error messages (why on earth would a bytea column complain about charset errors anyway?) Now that it's working, I though I'd share this little function. It's suitable for use in a script that's post-processing the txt files from running mysldump with the -T option. It takes one argument, a binary strong from a blob column, and returns the same data in an escaped form suitable for loading into a bytea column with the copy from function.

May it save you from a few of the headaches I have suffered.

sub encode_bytea { my ($blob) = @_; return join('', map { $_ = '\\\\' . sprintf("%.3o", $_) } unpack("C*", $blob)); }

No comments: