CHAR (fixed-length string)
Last updated: 2026-11-04
CHAR (fixed-length string)
CHAR(n) is the fixed-length character type: the value is stored padded to n characters, and trailing spaces are ignored in comparisons. It is not cheaper than VARCHAR in space terms — storing 'AB' in char(5) actually takes more room, because the padding occupies bytes too. It also brings the annoyance of values that look equal but are not, plus trailing-space confusion. Unless a protocol genuinely demands fixed width, prefer VARCHAR or TEXT.
| Kind | type |
|---|---|
| Example Language | SQL |
📝 Syntax
CHAR(n) | CHARACTER(n) —— fixed length, n from 1 to 10485760; omitting n means CHAR(1)
-- BPCHAR is the internal type name
Returns:Stores a fixed-length string and ignores trailing spaces in comparisons. CHAR beyond CHAR(1) is discouraged in PostgreSQL because padding and truncation behaviour rarely matches expectations.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
code | len | bytes | eq_ignores_padding | varchar_keeps_spaces | col_type
-------+-----+-------+--------------------+----------------------+-----------
AB | 2 | 5 | t | f | character
📌 Key Points
- length(code) returns 2 rather than 5 because padded trailing spaces do not count toward the length; only octet_length(code) reports the padded 5.
- code = 'AB' evaluates to true, showing that padding is ignored in comparisons, whereas 'AB' and 'AB ' differ in VARCHAR, where trailing spaces are significant data.
- Converting to text drops the padding: 'AB'::char(5)::text yields AB, so do not compare it against the original string.
- Trade-off: CHAR is discouraged in PostgreSQL. For a fixed-width protocol use VARCHAR(n) with a CHECK constraint, or TEXT with validation in the application.