VARCHAR (variable-length string)
Last updated: 2026-11-04
VARCHAR (variable-length string)
VARCHAR(n) is the variable-length string type, where n caps the maximum number of characters rather than fixing the length. Unlike CHAR it stores and compares values verbatim, so trailing spaces are preserved and significant. PostgreSQL lets you write VARCHAR with no n, which is then exactly equivalent to TEXT while keeping the VARCHAR name in place for a future length limit, which is a good way to express the intent "a limit may be added later".
| Kind | type |
|---|---|
| Example Language | SQL |
📝 Syntax
VARCHAR(n) | CHARACTER VARYING(n) —— variable length, up to n characters
VARCHAR —— unlimited length, equivalent to TEXT
Returns:Stores a variable-length string. With a declared n, an over-long value raises "value too long for type character varying(n)"; without n the length is unlimited.
▶ 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:
id | name | len | col_type
----+--------------------+-----+-------------------
1 | Alice | 5 | character varying
2 | Bob | 3 | character varying
3 | a longer user name | 18 | character varying
📌 Key Points
- The 20 in VARCHAR(20) is a ceiling, not a fixed width: 'Alice' occupies 5 characters and is never blank-padded to 20.
- When the length is omitted, information_schema reports a NULL maximum_length and the behaviour is identical to TEXT.
- Trade-off: use VARCHAR(n) when a ceiling encodes a real business rule; for plain strings use TEXT and skip the argument.