dbtrail blog
Back to the blog
PostgreSQL

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.

DG Daniel Guzman-Burgos November 24, 2026 5 min 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.

A char(5) value holding a and four spaces. In PostgreSQL 18, comparisons ignore the spaces, functions that take text remove them, and concat, format, JSON, LIKE and regular expressions keep them. MySQL 8.4 removes them when the value is read.One char(5) value, three behaviors in PostgreSQLIn MySQL 8.4 the same value loses its spaces when it is readStoreda····· = one trailing spacePostgreSQL 18Spaces ignored= GROUP BY DISTINCT UNIQUE→ trueSpaces removed|| upper() substr() length()→ aSpaces keptconcat() format() to_json() LIKE ~→ a····MySQL 8.4Spaces removed on read, before any operator→ a
One stored value, measured on PostgreSQL 18.7 and MySQL 8.4.13.

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

Which of four rows each query matches in PostgreSQL 18 and in MySQL 8.4. They agree on c = a, c = x and LIKE a%, and disagree on the other six.Which of the four rows each query matchesc is char(5); v and x hold the same strings as varchar and textPostgreSQL 18MySQL 8.41a2a··3”4·a1a2a··3”4·arow valuec = ‘a’c = ‘a ‘c = vc = xc LIKE ‘a’c LIKE ‘a%’c LIKE ‘%a’c LIKE ‘a____’c ~ ‘a$’row matchesno matchthe two databases disagree

Six of nine queries return different rows in the two databases.
  • A literal with trailing spaces. PostgreSQL ignores trailing spaces when it compares two char(n) values, and it reads an untyped literal as char(n), so c = '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 as latin1_swedish_ci, MySQL matches rows 1 and 2 as well.
  • Casting the literal. In PostgreSQL, c = 'a   '::text matches nothing and c = 'a   '::varchar matches rows 1 and 2. A varchar is converted to char; against text, the column is converted to text and the spaces count.
  • Joining to another column. That is why c = v and c = x differ in PostgreSQL even though v and x hold the same strings. MySQL gives rows 1, 3 and 4 for both.
  • Patterns. LIKE and regular expressions in PostgreSQL see the stored padding: 'a    ' does not end in a, 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.

ExpressionPostgreSQL 18MySQL 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,aa,a
length(c)11
octet_length(c) / LENGTH(c)51

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

ExpressionPostgreSQL 18MySQL 8.4
'abcdefgh'::char(5) / CAST(... AS CHAR(5))abcde, no warningabcde, warning 1292
'abcdef'::charan/a
'abc'::"char"an/a
insert 'abcdefgh' into cerror: value too longerror 1406 (strict mode)
insert 'abcde   ' into cstored as abcdestored 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 UNIQUE index on char(5) refuses 'a   ' after 'a'.
  • COUNT(DISTINCT c) is 3, where varchar and text count 4.
  • ORDER BY c returns '', ' a', 'a', 'a', 'abcde'.
  • Joining char(5) to char(3), and char(5) to text, 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 text or varchar(n) for new PostgreSQL columns. None of the differences above apply to them.
  • On an existing char(n) column, call rtrim(c) before LIKE '%x', a regular expression, concat(), format() or JSON output.
  • When you compare char(n) with text, write the cast you mean: c = t::char(5) ignores trailing spaces, c::text = t counts them.
  • When you move reports between MySQL and PostgreSQL, check every LIKE '%x' and regular expression on a char(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.

Leave a comment