Skip to main content
0 reactions

text vs varchar in PostgreSQL

Featured card for text vs varchar in PostgreSQL, comparing text (any length, nothing to check, same speed) with varchar(n) (up to n characters, refuses longer text, same speed).
text vs varchar in PostgreSQL.

text and varchar both store text in PostgreSQL. The difference is a length limit: varchar(n) refuses a value longer than n characters, and text takes any length. Here’s text vs varchar in PostgreSQL, with a small example of each.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
column_name text
column_name varchar(n)
  • column_name: the column’s name, inside a CREATE TABLE statement.
  • text: text of any length.
  • varchar(n): text of at most n characters. varchar is short for character varying, and without (n) it takes any length, just like text.

These come from Character Types (opens in a new tab) in the PostgreSQL documentation, which also says there’s no performance difference between them.

The Example

A chess club keeps its members in a table. A nickname may be at most 5 characters, so it’s varchar(5). A bio can be any length, so it’s text.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE club;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c club
OutputPlain text
You are now connected to database "club" as user "postgres".

Step 2: Create the Table

club=#SQL
CREATE TABLE members (nickname varchar(5), bio text);
OutputPlain text
CREATE TABLE

Step 3: Add a Member

club=#SQL
INSERT INTO members (nickname, bio) VALUES ('Sam', 'Plays chess every Sunday and has not missed a club night since 2019.');
OutputPlain text
INSERT 0 1

Sam fits in 5 characters, and the long bio is fine in a text column.

Step 4: Try a Nickname That’s Too Long

club=#SQL
INSERT INTO members (nickname, bio) VALUES ('Maximilian', 'Joined in 2026.');
OutputPlain text
ERROR:  value too long for type character varying(5)

Maximilian has 10 characters, so PostgreSQL refuses the whole row. Only Sam is in the table:

club=#SQL
SELECT * FROM members;
OutputPlain text
 nickname |                                 bio
----------+----------------------------------------------------------------------
 Sam      | Plays chess every Sunday and has not missed a club night since 2019.
(1 row)

Which One to Use

  • Use text for most text: names, titles, descriptions and emails.
  • Use varchar(n) only when a real rule limits the length, such as a 5-character nickname. The limit then protects your data.
  • Avoid char(n). It pads shorter values with spaces, and the documentation says it’s usually the slowest of the three.

Clean Up

To remove the example, connect to another database and drop the test database:

club=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE club;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
varchar(255) on every text column, a habit from other databasesIt’s no faster than text in PostgreSQL, and a value over 255 characters is refused.Use text, unless a real rule limits the length.
Expecting a cast to varchar(n) to fail like an insert doesA cast cuts the text instead: SELECT 'Maximilian'::varchar(5); returns Maxim.Let the column refuse the value, or check the length before you cast.

FAQ

Is text slower than varchar?

No. PostgreSQL stores them the same way, and varchar(n) only adds the length check.

Does n count characters or bytes?

Characters. varchar(5) takes five letters, even letters such as é that take more than one byte.

How long can a text value be?

About 1 GB, the limit the documentation (opens in a new tab) gives for any text value.

Conclusion

Use text for text, and varchar(n) only when a rule limits the length. Then PostgreSQL refuses a longer value, as it refused Maximilian. There’s no speed difference between the two.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments