-- UUID to BYTEA:

SELECT DECODE(REPLACE(CAST(uuid_field AS TEXT),'-',''), 'hex') FROM table;

-- BYTEA to UUID:

SELECT CAST(ENCODE(bytea_field, 'hex') AS UUID) FROM table;

-- BONUS --

-- UUID to base64 string (for nicer urls):

SELECT ENCODE(DECODE(REPLACE(CAST(uuid_field AS TEXT),'-',''), 'hex'), 'base64') FROM table;

-- base64 to UUID:

SELECT CAST(ENCODE(DECODE(base64_field, 'base64'), 'hex') AS UUID) FROM table;
