Sub topic 14
Binary Data with `bytea`
Assume you know basic SELECT, INSERT, and CREATE TABLE, and you can run queries in PostgreSQL. This chapter focuses on storing and retrieving raw binary bytes.
What `bytea` stores
bytea is PostgreSQL's binary string type. It stores a sequence of bytes, not characters. Use it for values such as encrypted data, compressed content, hashes, or small files.
A byte is an integer from 0 through 255. Binary data may not be valid text, so storing it in text can cause encoding errors or accidental conversions.
CREATE TEMP TABLE documents (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
filename text NOT NULL,
content bytea NOT NULL,
content_sha bytea
);
INSERT INTO documents (filename, content, content_sha)
VALUES (
'hello.txt',
convert_to('Hello, PostgreSQL!', 'UTF8'),
decode('48656c6c6f2c20506f737467726553514c21', 'hex')
);
SELECT
filename,
octet_length(content) AS bytes,
encode(content, 'escape') AS readable_content
FROM documents;convert_to converts text to bytes using a named character encoding. decode converts an encoded text representation into bytes. The encode function converts bytes back into text for display or transport.
PostgreSQL commonly displays bytea values in hexadecimal form. You can choose an output format explicitly:
encode(value, 'hex')produces two hexadecimal characters per byte.encode(value, 'base64')produces Base64 text, which is useful in APIs.encode(value, 'escape')uses an older escaped representation.
The encoded value is text. It is not the original binary value until you call decode.
Inspecting and comparing binary values
Use octet_length to count bytes. For binary data, length also counts bytes, but octet_length states the intention clearly.
SELECT
encode(content, 'hex') AS hexadecimal,
octet_length(content) AS byte_count,
content = decode('48656c6c6f2c20506f737467726553514c21', 'hex')
AS matches_expected
FROM documents
WHERE filename = 'hello.txt';You can calculate a digest with an extension such as pgcrypto. Store the digest as bytea when you need compact binary storage.
CREATE EXTENSION IF NOT EXISTS pgcrypto;
UPDATE documents
SET content_sha = digest(content, 'sha256');
SELECT
filename,
encode(content_sha, 'hex') AS sha256_hex
FROM documents;Choosing `bytea` instead of large objects
bytea works well when the value belongs directly to a row and is reasonably sized. It participates in normal transactions and backups.
PostgreSQL large objects provide a separate storage interface for very large values. They can be useful for streaming or external-style file access, but they require additional lifecycle management. Do not choose large objects merely because a value is binary.
How conversion works
Exercise
You receive the hexadecimal string 89504e47, which identifies the beginning of a PNG file. How do you store it as bytea and return it as Base64?
Show answer
Use decode('89504e47', 'hex') to create the binary value, then use encode(value, 'base64') to return its Base64 representation.