How to Convert a BYTEA Column to Text in PostgreSQL

byteapostgresql

how to convert a bytea column to text in PostgreSQL so that I can read the column properly in PGADMIN?

I have the following SQL query in the PGADMIN's query editor:

SELECT event_type, created_at, encode(metadata::bytea, 'escape') 
FROM public.events 
ORDER BY created_at DESC
LIMIT 100

However, it produces an encoded column with each records more or less ressembling the following output:

\203t\00000some_textd\000some_other_textd\0000

How can I get rid of this encoded, so that I only see the original value of the column, in the text format:

some_text some_data

What I have also tried:

SELECT event_id, event_type, created_at, decode((encode(metadata, 'escape')::text), 'escape')
FROM public.events
ORDER BY created_at DESC
LIMIT 100

But in the above case, the query returns a decode column of type bytea and I only see the field [binary data] for each record of the column.

I have also tried the first two answers mentioned here without success and can't properly translate the last answer to my query.

Best Answer

I finally found out how to display a bytea column as a text! I can use the function convert_from like so:

SELECT event_type, convert_from(metadata, 'UTF8') as metadata
FROM public.events 
ORDER BY created_at DESC
LIMIT 100

Then I'll have the metadata column in a human-readable format.