Sub topic 5
Converting to and from text, varchar and char
Assume you know basic SELECT, INSERT, and CREATE TABLE, and that you have used PostgreSQL types such as text, varchar, and char. This chapter focuses on how character types store values, how casts enforce length, and why trailing spaces can cause surprises.
Choosing a character type
PostgreSQL provides three commonly used character types:
| Type | Length rule | Typical use |
|---|---|---|
| `text` | Any length supported by PostgreSQL | General-purpose strings |
| `varchar(n)` | At most `n` characters | Values with a meaningful maximum length |
| `char(n)` | Exactly `n` characters after padding | Compatibility with fixed-width formats |
text and unconstrained varchar have the same practical storage behavior in PostgreSQL. A length limit on varchar(n) is a constraint, not a performance optimization.
char(n) is fixed-width. PostgreSQL pads shorter values with spaces, and comparisons generally ignore trailing spaces for these character types. This can make displayed values and stored values appear different.
CREATE TABLE contacts (
name text,
country_code varchar(2),
legacy_code char(4)
);
INSERT INTO contacts VALUES ('Ada', 'GB', 'A7');
SELECT name,
country_code,
legacy_code,
length(legacy_code) AS visible_length,
octet_length(legacy_code) AS stored_bytes
FROM contacts;The length function ignores trailing spaces in a char(n) value. octet_length can reveal the padded storage length.
Casting and length limits
Use CAST or PostgreSQL's :: operator to convert between character types:
SELECT CAST(42 AS text) AS standard_sql,
42::text AS postgres_syntax,
CAST('abc' AS varchar(10)) AS bounded_text;When converting to varchar(n), PostgreSQL checks the declared limit. A value that contains non-space characters beyond the limit raises an error when inserted or assigned. Explicit casts to fixed-width char(n) can truncate the value to the declared width, so use them only when that behavior is intended.
CREATE TABLE short_names (value varchar(3));
INSERT INTO short_names VALUES ('Ada'); -- succeeds
INSERT INTO short_names VALUES ('Grace'); -- fails: value is too longA cast does not format arbitrary text. For example, casting '1,234' to an integer does not mean “remove the comma.” Parsing formatted strings requires a formatting function such as to_number.
Common conversion errors
A string's apparent value may not match its character count. PostgreSQL measures characters, not bytes, for declared character lengths.
SELECT char_length('café') AS characters,
octet_length('café') AS bytes;The result depends on the database encoding, so multibyte characters can occupy more bytes than characters.
Trailing spaces also matter when converting fixed-width data. Before exporting or comparing padded values, remove unwanted padding explicitly:
SELECT rtrim(legacy_code) AS clean_code
FROM contacts;Exercise
Which type should you choose for a normal user-entered name, and which expression removes padding from a fixed-width code?
Show answer
Use text for the name, and use rtrim(code) to remove trailing spaces from the fixed-width code.