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".

Kindtype
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QMust VARCHAR always declare a length?
ANo. In PostgreSQL VARCHAR and TEXT are identical in storage, comparison and performance; the only reason to write VARCHAR(n) is to hard-code a business limit so that over-long writes fail at the database layer.
QDoes VARCHAR(20) truncate over-long strings?
ANo, it raises an error: ERROR: value too long for type character varying(20). Silently truncating requires an explicit expression such as name = left(name, 20).
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%

🙏 帮我们做得更好

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

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