EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL VARCHAR
 

PostgreSQL VARCHAR

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 15, 2023

PostgreSQL VARCHAR

 

 

Introduction to PostgreSQL VARCHAR

PostgreSQL supports various data types; the VARCHAR data type is used to store the characters. We can specify the number with VARCHAR to decide the length of the characters. The number defined with the VARCHAR data type is a positive integer number. The PostgreSQL throws an error if the text’s length is greater than the length of the VARCHAR data type defined. If we define the VARCHAR data type without the number as a limit, then it will store the text with an unlimited length of the text string with any size. The VARCHAR with a number defined to limit the number of characters like VARCHAR(n) is an acronym for the varying characters with length n.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Consider the following syntax to understand the VARCHAR data type

VARCHAR(n)
Or
VARCHAR

Explanation:

n: This defines the length of the text or the number of characters. It is a positive integer number. It defines the varying character with length n.

If the ‘n’ is not defined, then the column for which we have defined the VARCHAR data type will store the text string with an unlimited length of the text string of any size.

How Does VARCHAR Data Type Work in PostgreSQL?

1. The number within the parentheses should be defined after the VARCHAR datatype if we want to limit the store’s character size.

2. If we define the number with the VARCHAR data type, it will store the characters with the limit.

3. If we define the VARCHAR data type without the number as a limit, then it will store the text with an unlimited length of the text string with any size.

4. If the number is defined with VARCHAR data type, then PostgreSQL will check the characters’ length; if it exceeds, it will throw an exception.

5. If we try to insert a string with all spaces, in the end, PostgreSQL will truncate the spaces and allow it to store.

Examples to Implement PostgreSQL VARCHAR

Below are the examples of PostgreSQL VARCHAR:

Generally, for using the data type for characters, the VARCHAR is used, as it has the capability to store the values with variable length. Consider a table named TEXTS to understand the examples of the PostgreSQL VARCHAR data type.

Let’s create a table named TEXTS by using CREATE TABLE statement as follows,

Query:

CREATE table TEXTS
(
text_id serial PRIMARY KEY,
text VARCHAR (11) NOT NULL
);

Now insert some data in the TEXTS table by using the INSERT INTO statement as follows,

Query:

INSERT INTO TEXTS (text)
VALUES
('text 1'),
('Its text2'),
('t3');

Illustrate the content of the TEXTS table with the following SQL statement and snapshots.

Query:

select * from TEXTS;

Output:

PostgreSQL VARCHAR Example 1

The above example shows that we have a text column with a VARCHAR data type. We have inserted the value of the text with different length values.

Now, we will try to insert the text with a length greater than 11 in the text column of the TEXTS table as follows.

Query:

INSERT INTO TEXTS (text)
VALUES
('It is long text');

Output:

TEXTS Example 2

PostgreSQL throws an error if the text’s length is greater than the length of the VARCHAR data type defined.

Now, we will try to insert the text with a length greater than 11 and having spaces at the end in the text column of the TEXTS table as follows,

Query:

INSERT INTO TEXTS (text)
VALUES
('space            ');

Illustrate the result of the above statement after execution by using the following SQL statement and a snapshot.

Query:

select * from TEXTS;

Output:

PostgreSQL VARCHAR Example 3

We will add a column with data type VARCHAR without n and insert the values with different lengths.

Consider the following ALTER TABLE statement for adding a column with a VARCHAR data type.

ALTER TABLE TEXTS ADD COLUMN text_1 VARCHAR;

Illustrate the result of the above statement by using the following snapshot.

Now, we will insert the text string with variable lengths in the text_1 column by using the INSERT INTO statement as follows,

Query:

INSERT INTO TEXTS (text,text_1)
VALUES
('t1','This is text_1 first'),
('t1','This is text_1 long text' ),
('t1','This is text_1 long long long text string');
select * from TEXTS;

Output:

String Example 4

The above example shows that the column text_1 has text strings with a variable-length size or any size. It will almost accept the string with unlimited size. Whenever we want to store the free text data, we generally prefer to use the data type VARCHAR without the defined number, which specifies the limit.

Conclusion

We hope you understand the PostgreSQL VARCHAR data type from the above article. Also, we have explained the working of the VARCHAR data type., With the help of examples and syntax explained, we hope you have understood the VARCHAR data type in detail.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL VARCHAR” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Introduction to PostgreSQL Timestamp
  2. How to Notify Works in PostgreSQL?
  3. PostgreSQL JSON (Examples)
  4. Introduction to PostgreSQL Timestamp

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW