PostgreSQL – Difference between CHAR, VARCHAR and TEXT
Last Updated :
20 Aug, 2024
When working with textual data in PostgreSQL, choosing the appropriate character data type is essential for performance and data integrity. PostgreSQL offers three primary character data types: ‘CHAR’, ‘VARCHAR’, and ‘TEXT’. While these data types might seem similar, they have distinct differences that impact how they store and manage string data.
What is CHAR data type?
The CHAR data type is used to store fixed-length strings. It is represented as ‘CHAR(n)’ or ‘CHARACTER(n)’ in PostgreSQL, where n specifies the length of the string. If ‘n’ is not provided, it defaults to ‘CHAR(1)’. The CHAR data type pads the stored value with spaces if the input is shorter than the defined length.
What is VARCHAR data type?
This data type is used to store characters of limited length. It is represented as varchar(n) in PostgreSQL, where n represents the limit of the length of the characters. If n is not specified it defaults to varchar which has unlimited length. Unlike ‘CHAR‘, ‘VARCHAR‘ does not pad stored values with extra spaces.
What is TEXT data type?
This data type is used to store characters of unlimited length. It is represented as text in PostgreSQL. The performance of the varchar (without n) and text are the same. There is no specific length restriction, making it suitable for storing large text blocks like articles, descriptions, or logs.
Key Differences Between CHAR, VARCHAR, and TEXT in PostgreSQL
The below table provides with the major difference between CHAR, VARCHAR, and TEXT:
Comparison |
CHAR |
VARCHAR |
TEXT |
Syntax |
'CHAR(n)' |
'VARCHAR(n)' |
'TEXT' |
Representation |
‘CHARACTER(n)’ or ‘CHAR(n)’ |
'VARCHAR(n)' |
'TEXT' |
Length of Characters |
Fixed (pads with spaces) |
Variable (up to ‘n’) |
Unlimited |
Spaces and Padding |
Pads with spaces |
No padding |
No padding |
If n Not Specified |
Defaults to ‘CHAR(1)’ |
Defaults to unlimited |
No concept of ‘n’ |
Query Cycle |
Extra cycle to check length |
Extra cycle to check length |
No evaluation of length |
Performance |
Slightly slower due to padding |
Better for varying lengths |
Ideal for large texts |
Example: Demonstrating CHAR, VARCHAR, and TEXT in PostgreSQL
Let’s create a new table(say, ‘character_tests’) for the demonstration using the below commands:
CREATE TABLE character_tests (
id serial PRIMARY KEY,
x CHAR (1),
y VARCHAR (10),
z TEXT
);
Now let’s insert a new row into the ‘char_test’ table using the below command:
INSERT INTO character_tests (x, y, z)
VALUES
(
'Geeks',
'This is a test for varchar',
'This is a very long text for the PostgreSQL text column'
);
At this stage PostgreSQL will raise an error as the data type of the ‘x’ column is ‘CHAR(1)’ and we tried to insert a string with three characters into this column as shown below:
ERROR: value too long for type character(1)

So, now let’s fix it.
INSERT INTO character_tests (x, y, z)
VALUES
(
'G',
'This is a test for varchar',
'This is a very long text for the PostgreSQL text column'
);
Now, we will get the same error for the y column as the number of characters entered is greater than 10 as shown below:
ERROR: value too long for type character(10)

Let’s fix it too.
INSERT INTO character_tests (x, y, z)
VALUES
(
'Y',
'varchar(n)',
'This is a very long text for the PostgreSQL text column'
);
Now that we have managed to successfully assign the values to the character data type, check it by running the below command:
SELECT * FROM character_tests;
Output:

Conclusion
In conclusion, choosing between ‘CHAR’, ‘VARCHAR’, and ‘TEXT’ in PostgreSQL depends on the specific requirements of your application, that is, use ‘CHAR’ for fixed-length data where padding is acceptable, use ‘VARCHAR’ for variable-length data with defined constraints and use ‘TEXT’ for unlimited-length strings or when there is no clear maximum length.
Similar Reads
PostgreSQL - VARCHAR Data Type
In the world of relational databases, PostgreSQL stands out with its robust support for various data types, including the flexible VARCHAR data type. This character data type allows us to store strings of variable length, making it an essential choice for many applications. In this article, we will
3 min read
CHARACTER VARYING vs VARCHAR in PostgreSQL
In PostgreSQL, the terms CHARACTER VARYING and VARCHAR are often used interchangeably, but are they truly the same? We will understand these data types in this article to clarify their similarities and differences. We'll explore how they work, their syntax, and examples of their usage in PostgreSQL.
6 min read
What is an Index in PostgreSQL?
PostgreSQL is a powerful and reliable open-source relational database management system (RDBMS) known for its extensive features, including robustness and scalability. One key feature of PostgreSQL that contributes to its high performance is indexing. Proper use of indexes can significantly improve
5 min read
PostgreSQL - TEXT Data Type
PostgreSQL provides a highly flexible character data type known as TEXT, designed to store character strings of virtually unlimited length. Unlike the VARCHAR data type, which can be limited to a specified length, the TEXT data type offers the same efficiency and performance without the length const
3 min read
PostgreSQL - CHAR Data Type
The CHAR data type in PostgreSQL is one of the essential character data types for storing fixed-length strings. Unlike VARCHAR, which stores variable-length data, CHAR is used when we need to store a fixed-length string. This article will explain the CHAR data type in PostgreSQL, its syntax, common
5 min read
PostgreSQL - INTEGER Data Type
In PostgreSQL, the INTEGER data type is widely used for storing numerical data efficiently. It is a 4-byte data type that allows us to store whole numbers within a specified range, making it ideal for various use cases like population counts, active user statistics, and more. In this article, we wil
4 min read
PostgreSQL - Date Data Type
PostgreSQL offers powerful DATE data type and date functions to efficiently handle date and time information. PostgreSQL DATE data type allows for storing and manipulating calendar dates while its robust set of date functions enables users to perform operations like date arithmetic and formatting. I
4 min read
PostgreSQL - Cheat Sheet : Basic to Advanced
PostgreSQL is a powerful, open-source object-relational database management system (ORDBMS). It is designed to help developers build robust applications and allow administrators to maintain data integrity while creating fault-tolerant environments. Like other enterprise databases such as Microsoft S
5 min read
PostgreSQL - NUMERIC Data Type
In PostgreSQL, the NUMERIC data type is designed for high-precision number storage by making it ideal for financial and scientific applications where accuracy is critical. It supports a large number of digits both before and after the decimal point, minimizing rounding errors. Understanding the nuan
5 min read
DECIMAL vs NUMERIC Datatype in PostgreSQL
PostgreSQL is a powerful open-source relational database management system known for its robustness, extensibility, and adherence to SQL standards. When it comes to storing numeric data, PostgreSQL offers two main datatypes: DECIMAL and NUMERIC. While these datatypes are often used interchangeably,
3 min read