Sub topic 1
Character types: `text`, `varchar` and `char(n)`
Prerequisites
This chapter assumes that you can write basic SELECT, WHERE, and GROUP BY queries. You should also understand PostgreSQL data types and the difference between a value and NULL.
PostgreSQL provides three commonly used character types:
textstores a string with no declared maximum length.varchar(n)stores a string with a maximum ofncharacters.char(n)stores a fixed-width string of exactlyncharacters.
The choice usually describes a validation rule, not a performance requirement.
`text` and `varchar(n)`
text is the normal choice for unrestricted text, such as a product description or an email message. PostgreSQL does not impose a practical short limit on the declaration.
Use varchar(n) when the maximum is part of the data model. For example, a country code might have a limit of two characters, while a username might have a limit of 30.
CREATE TEMP TABLE accounts (
username varchar(30),
biography text
);
INSERT INTO accounts (username, biography)
VALUES ('mira', 'A short profile description');
SELECT username, biography
FROM accounts;If an inserted value exceeds the varchar(30) limit, PostgreSQL raises an error. This makes the limit useful as a database constraint. It prevents invalid data even when another application writes to the table.
text and varchar use the same general storage behavior in PostgreSQL. A plain varchar without (n) is also effectively unlimited. Therefore, choose varchar(n) for its enforced limit, not because it is faster.
Fixed-width `char(n)`
char(n), also called character(n), is designed for values whose width is always fixed. PostgreSQL pads shorter values with spaces.
CREATE TEMP TABLE labels (
code char(5),
name varchar(5)
);
INSERT INTO labels (code, name)
VALUES ('AB', 'AB');
SELECT
code,
name,
char_length(code) AS code_characters,
octet_length(code) AS code_bytes
FROM labels;The code value is stored as a five-character field, while name stores only the characters provided. PostgreSQL treats trailing spaces in char(n) values specially. Comparisons ignore those padding spaces, and many string operations remove them from the logical value.
A fixed-width type can make sense for legacy file formats or codes that are always padded to a known width. It is usually a poor choice for names, identifiers, or user-entered text because padding can produce surprising results.
| Type | Length rule | Typical use |
|---|---|---|
| `text` | No declared limit | Descriptions and general text |
| `varchar(n)` | At most `n` characters | Values with a business maximum |
| `char(n)` | Fixed-width, space-padded | Legacy fixed-width codes |
Choosing a type
Do not use a character type to enforce a pattern such as “two uppercase letters.” A length declaration checks length only. Use a CHECK constraint for the pattern.
CREATE TABLE country_codes (
code varchar(2) NOT NULL
CHECK (code ~ '^[A-Z]{2}$')
);This constraint requires exactly two uppercase ASCII letters. If the rule should allow other alphabets or Unicode case behavior, define that rule explicitly rather than assuming that upper() or a length limit is sufficient.
Exercise
Which type should you choose for a customer display name, a username limited to 40 characters, and a legacy five-column fixed-width code?
Show answer
Use text for the display name, varchar(40) for the username, and char(5) for the legacy fixed-width code.