Base64 in SQL
How to encode and decode Base64 in SQL — with copy-paste examples and a live converter to check your output.
Base64 functions differ by database: MySQL/MariaDB use TO_BASE64/FROM_BASE64, while PostgreSQL uses ENCODE/DECODE with an explicit "base64" format argument.
Encode to Base64 in SQL
-- MySQL / MariaDB
SELECT TO_BASE64('Hello, World!');
-- PostgreSQL
SELECT ENCODE('Hello, World!'::bytea, 'base64');Decode Base64 in SQL
-- MySQL / MariaDB
SELECT FROM_BASE64(encoded);
-- PostgreSQL
SELECT DECODE(encoded, 'base64');Notes & gotchas
MySQL's TO_BASE64 inserts a newline every 76 characters (MIME-style); wrap with REPLACE(..., '\n', '') if you need a single line. PostgreSQL's DECODE returns bytea, so cast with ::text or convert_from() to get a readable string back.
Try it live
Base64 in SQL — FAQ
How do I Base64 encode a string in SQL?+
Use the code shown above. MySQL's TO_BASE64 inserts a newline every 76 characters (MIME-style); wrap with REPLACE(..., '\n', '') if you need a single line. PostgreSQL's DECODE returns bytea, so cast with ::text or convert_from() to get a readable string back.
How do I decode Base64 back to text in SQL?+
Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.
Is Base64 encoding the same across programming languages?+
Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.
Does Base64 secure my data?+
No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.