Site icon JD Bots

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

Syntax

column_name text
column_name varchar(n)

These come from Character Types 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=#

CREATE DATABASE club;

Output

CREATE DATABASE

postgres=#

\c club

Output

You are now connected to database "club" as user "postgres".

Step 2: Create the Table

club=#

CREATE TABLE members (nickname varchar(5), bio text);

Output

CREATE TABLE

Step 3: Add a Member

club=#

INSERT INTO members (nickname, bio) VALUES ('Sam', 'Plays chess every Sunday and has not missed a club night since 2019.');

Output

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=#

INSERT INTO members (nickname, bio) VALUES ('Maximilian', 'Joined in 2026.');

Output

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=#

SELECT * FROM members;

Output

 nickname |                                 bio
----------+----------------------------------------------------------------------
 Sam      | Plays chess every Sunday and has not missed a club night since 2019.
(1 row)

Which One to Use

Clean Up

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

club=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE club;

Output

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 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

Related

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

Exit mobile version