Don’t use char(n) in PostgreSQL. Ever.
The same char(5) table in both databases. PostgreSQL ignores, removes or keeps the padding depending on the operator; MySQL removes it on read.

A char(5) column pads a shorter value with spaces up to five characters. PostgreSQL and MySQL both have the type. I ran the same table and the same queries on PostgreSQL 18.7 and MySQL 8.4.13, because we needed a column-store copy of PostgreSQL tables to answer exactly as PostgreSQL does, and char(n) kept producing differences.
In MySQL the padding is gone once the value is read, so every operator works on a. In PostgreSQL what an operator sees depends on the operator, in the three groups of the drawing above.
The test table
Both databases got the same four rows. PostgreSQL runs with the en_US.UTF-8 locale; MySQL with its 8.4 default collation, utf8mb4_0900_ai_ci.
CREATE TABLE t (id int, c char(5), v varchar(5), x text);
INSERT INTO t VALUES
(1, 'a', 'a', 'a'),
(2, 'a ', 'a ', 'a '),
(3, '', '', ''),
(4, ' a', ' a', ' a');
Rows 1 and 2 differ only in trailing spaces. In the char(5) column they hold the same value; in varchar and text they stay different.
Which rows match
- A literal with trailing spaces. PostgreSQL ignores trailing spaces when it compares two
char(n)values, and it reads an untyped literal aschar(n), soc = 'a 'matches rows 1 and 2. MySQL 8.4’s default collation is NO PAD, so the same literal matches nothing. Under a PAD SPACE collation such aslatin1_swedish_ci, MySQL matches rows 1 and 2 as well. - Casting the literal. In PostgreSQL,
c = 'a '::textmatches nothing andc = 'a '::varcharmatches rows 1 and 2. Avarcharis converted tochar; againsttext, the column is converted totextand the spaces count. - Joining to another column. That is why
c = vandc = xdiffer in PostgreSQL even thoughvandxhold the same strings. MySQL gives rows 1, 3 and 4 for both. - Patterns.
LIKEand regular expressions in PostgreSQL see the stored padding:'a 'does not end ina, and it is exactly five characters long.LIKE 'a%'returns the same rows in both because the%covers the spaces, which is why the most common pattern does not show the problem.
Reading the value back
The same row 1, through different expressions. · marks a trailing space.
| Expression | PostgreSQL 18 | MySQL 8.4 |
|---|---|---|
'[' || c || ']' | [a] | n/a |
concat('[', c, ']') | [a····] | [a] |
format('[%s]', c) | [a····] | n/a |
to_json(c) / JSON_QUOTE(c) | "a····" | "a" |
row_to_json(t) / JSON_OBJECT('c', c) | {"c":"a····"} | {"c": "a"} |
array_agg(c) on rows 1 and 2 | {"a····","a····"} | n/a |
string_agg(c, ',') / GROUP_CONCAT(c) | a,a | a,a |
length(c) | 1 | 1 |
octet_length(c) / LENGTH(c) | 5 | 1 |
In PostgreSQL, || converts the column to text first and the conversion drops the spaces; concat(), format(), JSON and arrays print the stored value. upper(), substr(), right(), position() and md5() take text, so they gave the same results as MySQL on all four rows.
Casts and inserts
| Expression | PostgreSQL 18 | MySQL 8.4 |
|---|---|---|
'abcdefgh'::char(5) / CAST(... AS CHAR(5)) | abcde, no warning | abcde, warning 1292 |
'abcdef'::char | a | n/a |
'abc'::"char" | a | n/a |
insert 'abcdefgh' into c | error: value too long | error 1406 (strict mode) |
insert 'abcde ' into c | stored as abcde | stored as abcde |
In PostgreSQL, char without a length is char(1). "char", in quotes, is a separate one-byte internal type.
Where they agree
- A
UNIQUEindex onchar(5)refuses'a 'after'a'. COUNT(DISTINCT c)is 3, wherevarcharandtextcount 4.ORDER BY creturns'',' a','a','a','abcde'.- Joining
char(5)tochar(3), andchar(5)totext, matches the same rows.
What PostgreSQL’s documentation says
The three behaviors are documented. Trailing spaces “are treated as semantically insignificant and disregarded when comparing two values of type character“. They “are removed when converting a character value to one of the other string types”. And they are “semantically significant” in pattern matching, “that is LIKE and regular expressions”. The same page says character(n) “is usually the slowest of the three because of its additional storage costs”, and that “in most situations text or character varying should be used instead”.
On the MySQL side, the PAD_CHAR_TO_FULL_LENGTH SQL mode, which keeps the padding on read, is deprecated. MySQL 8.4.13 still accepts it.
Recommendations
- Use
textorvarchar(n)for new PostgreSQL columns. None of the differences above apply to them. - On an existing
char(n)column, callrtrim(c)beforeLIKE '%x', a regular expression,concat(),format()or JSON output. - When you compare
char(n)withtext, write the cast you mean:c = t::char(5)ignores trailing spaces,c::text = tcounts them. - When you move reports between MySQL and PostgreSQL, check every
LIKE '%x'and regular expression on achar(n)column: in this test, PostgreSQL returned no rows where MySQL returned three.
The test was the table above plus one query per line of each chart and table. Results can change with the collation.
Get new posts by email
One email when a new post is out. Nothing else, and you can unsubscribe from any email.