9bNine Bits

PostgreSQL text vs varchar: Which Should You Use?

In PostgreSQL, use text for most columns. The main difference from varchar(n) is that varchar(n) enforces a maximum length when a value is written, and text does not. If you need a length rule, put it in a CHECK constraint or a domain over text. Use varchar(n) only when you want a short, stable limit visible in the column definition, and avoid char(n) in new schemas.
PostgreSQL text vs varchar: Which Should You Use?

Decision summary: text, varchar(n), varchar or char(n)

Default to text. It is PostgreSQL's native string type, and a length rule can be added to it separately with a CHECK constraint or a domain, so the rule can change without touching the column type. Choose varchar(n) when the limit is short and stable and you want it declared on the column itself, for example a fixed-format code. Choose char(n) only for genuinely fixed-width data, because it is physically space-padded.

Criteriontextvarchar(n)varchar (no n)char(n)
LengthVariable, unlimitedVariable, up to n charactersVariable, any lengthFixed n characters, space-padded
Over-length writeNo limit to exceedError, except all-space excess is truncated and an explicit cast truncates silentlyNo limit to exceedSame rule as varchar(n)
Maximum declared nNot applicable10,485,760Not applicable10,485,760
Trailing spacesSignificantSignificantSignificantDisregarded when comparing char values
Built-in string functionsNative type: most functions take and return textActs like a domain over textActs like a domain over textPadded, blank-trimmed semantics
SQL standardNot in the standardStandard (character varying)Standard aliasStandard (character); bpchar is the PostgreSQL extension
Typical useFree-form content, rules in CHECK or a domainShort codes with a fixed business limitRarely needed; text says the same thingFixed-width legacy data

What each type accepts

varchar(n) and char(n) store strings up to n characters, not bytes, which matters for multibyte encodings. A declared n must be greater than zero and cannot exceed 10,485,760. text has no declared limit, and neither does varchar written without a length.

On write, an over-length value sent to varchar(n) or char(n) raises an error, with two documented exceptions. If the excess characters are all spaces, the value is truncated to n instead of rejected. And an explicit cast to varchar(n) or char(n) truncates an over-length value without any error:

SELECT 'abcdef'::varchar(3);   -- returns 'abc', no error
SELECT 'abc   '::varchar(3);   -- all-space excess, truncated to 'abc'

The cast behavior is the one that surprises people: code that casts before inserting bypasses the length check entirely.

Why text is the default

text is PostgreSQL's native string data type. Most built-in functions that operate on strings are declared to take or return text, not character varying. For many purposes, character varying acts as though it were a domain over text, so declaring varchar(n) adds a validity check on top of a text-like value rather than creating a separate kind of string.

That framing points to the design rule: if varchar(n) is effectively text plus a check, you can write that check yourself, where it is easier to change and reuse. Varchar without a length accepts strings of any length, so it expresses the same intent as text, but text is the type the built-in functions use.

Where a length limit belongs: column type, CHECK or domain

A length limit is a business rule, and there are three places to put it:

  • varchar(n) puts the rule on the column. It is visible in the table definition and in \d output, which suits short, stable limits.
  • A CHECK constraint on a text column keeps the storage type separate from the rule. It can be dropped and replaced without altering the column type.
  • A domain bundles text and a CHECK into one reusable type, so a rule shared by many tables is defined once.
CREATE DOMAIN sku_code AS text
    CHECK (char_length(VALUE) <= 20);

CREATE TABLE products (
    id    bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    sku   sku_code NOT NULL,
    name  text NOT NULL,
    notes text,
    country_code varchar(2) NOT NULL,
    CONSTRAINT notes_length CHECK (char_length(notes) <= 2000)
);

Here the SKU rule lives in a domain shared across tables, the notes rule is a named table constraint, and the two-letter country code uses varchar(n) because that limit will not change. Because a CHECK on text is a separate object, it also gives you a named constraint to report in error messages.

Changing a length limit

How you change a limit depends on where you put it. With a CHECK constraint, you replace the constraint and leave the column type alone. With varchar(n), you alter the column type.

-- Limit enforced by a CHECK constraint: drop and re-add in one statement
ALTER TABLE products
    DROP CONSTRAINT notes_length,
    ADD CONSTRAINT notes_length CHECK (char_length(notes) <= 5000);

-- Limit declared on the column: widen it, or remove it by moving to text
ALTER TABLE products ALTER COLUMN country_code TYPE varchar(3);
ALTER TABLE products ALTER COLUMN country_code TYPE text;

Narrowing a limit only succeeds if every existing value already fits, so look for offenders first:

SELECT count(*) FROM products WHERE char_length(notes) > 1000;

Before altering a column type on a large production table, check which indexes, views and other objects depend on the column, and rehearse the change on a copy of the data. Changing a limit in a domain follows the same pattern: alter the domain's constraint rather than each column that uses it.

Moving off char(n)

char(n) pads every value with spaces to n characters, and trailing spaces are disregarded when comparing char values. In varchar and text, trailing spaces are semantically significant, including under LIKE and regular expression matching. A column converted from char(n) to text or varchar(n) can therefore start behaving differently: 'ab' and 'ab ' compare equal as char(n) but are different values as text.

Trim or normalize values as part of the migration and test the queries that filter or join on the column. If you want blank-trimmed behavior with no length limit, bpchar without a length specifier accepts strings of any length and treats trailing spaces as semantically insignificant.

Common questions

What is the difference between text and varchar in PostgreSQL?

text has no length limit. varchar(n) accepts up to n characters and raises an error on longer values, except that all-space excess is truncated and an explicit cast to varchar(n) truncates silently. varchar without n accepts any length.

Should I use varchar(255) in PostgreSQL?

Only if 255 is a real business rule. Otherwise use text and add a CHECK constraint or domain if you need a limit. The declared length is a validity check, and the maximum allowed n is 10,485,760.

When does char(n) make sense?

For genuinely fixed-width data or strict standard compatibility. char(n) values are space-padded and trailing spaces are disregarded in comparison, which is rarely what new schemas want.

Are lengths counted in bytes or characters?

Characters. A declared n in varchar(n) or char(n) is the maximum number of characters, not bytes, so multibyte text is counted by character.

Sources

  1. PostgreSQL Documentation: Character Types
  2. PostgreSQL Press FAQ

About this page

Last reviewed November 25, 2026 by the Nine Bits editors. Read our editorial policy or report an error.