decode()

A function for converting data encoded as text to bytea

decode() is a system function for converting a textual representation of binary data into bytea.

decode() was added in PostgreSQL 7.2.

Usage

decode ( string text, format text ) → bytea

format can be one of the following values:

  • base64
  • escape
  • hex

Change history

Examples

Usage examples for decode() for each format type.

base64

Using decode() to convert a base64 value to bytea:

postgres=# SELECT decode('3q2+7w==', 'base64');
   decode   
------------
 \xdeadbeef
(1 row)

escape

Using decode() to convert an escape value to bytea:

postgres=# SELECT decode('\336\255\276\357', 'escape');
   decode   
------------
 \xdeadbeef
(1 row)

hex

Using decode() to convert a hexadecimal value to bytea:

postgres=# SELECT decode('deadbeef', 'hex');
   decode   
------------
 \xdeadbeef
(1 row)

Categories

String manipulation, System function

See also

encode()