I have problem how to convert column using plpgsql (bytea
-> text
). I wrote function which works on some databases and doesn't for others. I don't know how to fix it.
Using databases from 8.0 - 9.3; this error is for 8.1.19.
I received:
ERROR: column "the_column" cannot be cast to type "text" CONTEXT: SQL statement "ALTER TABLE the_table ALTER COLUMN the_column TYPE text" PL/pgSQL function "byteatotext" line 11 at execute statement
My function:
CREATE OR REPLACE FUNCTION byteaToText()
RETURNS text AS
$BODY$
DECLARE
ver int;
BEGIN
SELECT into ver (select setting from pg_settings where name='server_version_num') as test;
IF ver < 80200 THEN
EXECUTE 'ALTER TABLE the_table ALTER COLUMN the_column TYPE text USING ENCODE(properties, \'escape\'))';
RETURN ver;
ELSE
EXECUTE 'ALTER TABLE the_table ALTER COLUMN the_column TYPE text';
RETURN ver;
END IF;
RETURN 'error';
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
select byteaToText();