w3resource

Decoding Base64 Encoded Data in PostgreSQL


PostgreSQL Base64 Decode: Converting Encoded Data Back to Original

In PostgreSQL, Base64 decoding is used to convert data that was encoded in Base64 format back to its original form. Base64 encoding is a common method for representing binary data in ASCII text, which can be helpful for storing binary data in databases. PostgreSQL provides built-in functions to decode Base64 data, allowing you to retrieve the original content when needed.

Syntax for Decoding Base64 in PostgreSQL:

To decode a Base64 encoded string, use the decode() function:

decode(string, format)

Parameters:

  • string: The Base64 encoded string.
  • format: The format type; use 'base64' to specify Base64 encoding.

Example usage of decode with Base64

-- Example of decoding a Base64 encoded string
-- Decode the Base64 string
SELECT decode('U29tZSBzYW1wbGUgdGV4dA==', 'base64') AS original_text;

Explanation:

  • The decode() function takes the Base64 encoded string 'U29tZSBzYW1wbGUgdGV4dA==' and the format 'base64'.
  • It returns the original text in binary format, which PostgreSQL will display in a readable form if it's ASCII or text data.

Output:

original_text
--------------
Some sample text

Additional Example: Decoding Stored Base64 Data

If you have a column that stores data in Base64 format, you can decode it as follows:

Code:

-- Creating a sample table
CREATE TABLE encoded_data (
    id SERIAL PRIMARY KEY,
    data_base64 TEXT
);

-- Inserting Base64 encoded data
INSERT INTO encoded_data (data_base64)
VALUES ('U29tZSBkYXRhIHRvIGJlIGRlY29kZWQ=');

-- Decoding the data
SELECT id, decode(data_base64, 'base64') AS decoded_data
FROM encoded_data;

Explanation:

  • A table encoded_data is created to hold Base64-encoded text.
  • The decode() function is applied to the data_base64 column to decode the text back to its original form.
  • The query retrieves the decoded data alongside the id field.

Result:

id | decoded_data
-----------------
1  | Some data to be decoded

Practical Uses of Base64 Decoding in PostgreSQL:

1. Storing and Retrieving Binary Data:

Base64 encoding is commonly used for handling binary data (such as images or files) as text. Decoding allows you to retrieve the original binary content for processing or display.

2. Data Interchange Between Systems:

When systems exchange data in encoded formats (e.g., via APIs), decoding in PostgreSQL enables seamless integration.

3. Securing Sensitive Data:

Though not a security measure, Base64 encoding can mask data visibility. Decoding in PostgreSQL lets you retrieve the original values for users with the appropriate permissions.

Summary:

Decoding Base64 encoded data in PostgreSQL is straightforward using the decode() function. This function is essential when handling encoded binary data, especially in databases that support multiple formats and data interchange between systems.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/PostgreSQL/snippets/postgresql-base64-decode.php