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
column_name text
column_name varchar(n)
column_name: the column’s name, inside aCREATE TABLEstatement.text: text of any length.varchar(n): text of at mostncharacters.varcharis short forcharacter varying, and without(n)it takes any length, just liketext.
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:
CREATE DATABASE club;
CREATE DATABASE
\c club
You are now connected to database "club" as user "postgres".
Step 2: Create the Table
CREATE TABLE members (nickname varchar(5), bio text);
CREATE TABLE
Step 3: Add a Member
INSERT INTO members (nickname, bio) VALUES ('Sam', 'Plays chess every Sunday and has not missed a club night since 2019.');
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
INSERT INTO members (nickname, bio) VALUES ('Maximilian', 'Joined in 2026.');
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:
SELECT * FROM members;
nickname | bio
----------+----------------------------------------------------------------------
Sam | Plays chess every Sunday and has not missed a club night since 2019.
(1 row)
Which One to Use
- Use
textfor 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:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE club;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
varchar(255) on every text column, a habit from other databases | It’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 does | A 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
Related
- PostgreSQL Tutorial — Part 5: Data Types Explained
- How to Store Money in PostgreSQL: numeric vs money
- How to Check a Column’s Data Type in PostgreSQL
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