Sub topic 5
Text, varchar and char
This chapter assumes basic SELECT, INSERT, and CREATE TABLE, plus familiarity with columns and constraints. PostgreSQL provides three closely related character types:
textstores strings of any length.varchar(n)stores strings up toncharacters.char(n)stores fixed-width strings of exactlyncharacters.
In most schemas, text is the best default. Use a length constraint when the length has business meaning, not merely because another database requires it.
The three character types
text and unbounded varchar have the same practical storage behavior in PostgreSQL. Both store variable-length strings. The following declarations are usually equivalent:
CREATE TABLE users (
username text NOT NULL,
display_name varchar NOT NULL
);varchar(30) adds a maximum length. PostgreSQL rejects a value longer than 30 characters:
CREATE TABLE products (
sku varchar(20) PRIMARY KEY,
name text NOT NULL,
short_description varchar(160)
);
INSERT INTO products (sku, name, short_description)
VALUES ('BK-001', 'Database Basics', 'A practical introduction');The limit counts characters, not bytes. A multibyte character such as é counts as one character. PostgreSQL raises an error if an inserted or updated value exceeds the declared limit.
char(n), also called character(n), is fixed-width. PostgreSQL pads shorter values with spaces when storing them. Comparisons generally ignore trailing spaces, but conversion back to text can expose padding behavior. This makes char(n) surprising for ordinary names and descriptions.
CREATE TABLE legacy_codes (
country_code char(2) NOT NULL,
label text NOT NULL
);Use char(n) only when fixed-width semantics are required by a legacy format or an external protocol. For a two-letter code, char(2) can express the length, but text CHECK (char_length(code) = 2) is often easier to reason about.
| Type | Length behavior | Typical use |
|---|---|---|
| `text` | Any length | Names, messages, descriptions |
| `varchar(n)` | At most `n` characters | A meaningful maximum, such as an external field limit |
| `char(n)` | Fixed width with padding | Legacy fixed-width data |
Constraints and comparisons
A type limit is not a substitute for validation. If a value must have a particular format, add a constraint:
CREATE TABLE accounts (
email text NOT NULL,
username text NOT NULL,
CONSTRAINT username_length CHECK (
char_length(username) BETWEEN 3 AND 30
)
);char_length counts characters. length also counts characters for text values. Neither function measures the number of bytes.
PostgreSQL compares text according to the column's collation, which controls rules such as sorting and case behavior. Ordinary equality is case-sensitive: 'Admin' and 'admin' are different values. If an application needs case-insensitive identity, choose an explicit design, such as storing a normalized lowercase value or using the citext extension.
A useful mental model
Common errors
- Using
char(n)for ordinary user input and then being surprised by padding. - Treating
varchar(n)as a performance optimization. - Assuming text comparison ignores letter case.
- Counting bytes when the requirement is a character count.
- Omitting
NOT NULLwhen an empty value and a missing value have different meanings.
Exercise
Which type should you choose for a customer biography, a postal code with a real maximum of 12 characters, and a legacy field that must occupy 10 characters?
Show answer
Use text for the biography, varchar(12) for the postal code, and char(10) for the legacy fixed-width field. Add format or content constraints where needed.