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.

Kindtype
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QDoes CHAR really save space over VARCHAR?
ANot in PostgreSQL. CHAR pads the value to the declared length and those padding bytes cost just as much, so storing 'AB' in char(5) takes more room than varchar(5), while the semantic pitfalls are very real.
QWhat happens when CHAR(n) gets an over-long value?
AThe two paths differ: writing an over-long value into the column raises "value too long for type character(3)", while the explicit cast 'ABCDEF'::char(3) silently truncates to ABC — and data lost to a cast is much harder to notice.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏