PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL Character Types: CHAR, VARCHAR, and TEXT

PostgreSQL Character Types: CHAR, VARCHAR, and TEXT

Summary: in this tutorial, we will introduce you to the PostgreSQL character data types including char, varchar, and text, and give you some tips to choose the right one to design the database tables.

Introduction to the PostgreSQL character types

PostgreSQL provides three primary character types: character(n) or char(n), character varying(n) or varchar(n), and text, where n is a positive integer.

Character TypesDescription
character varying(n), varchar(n)variable-length with limit
character(n), char(n)fixed-length, blank padded
text, varcharvariable unlimited length

Both char(n) and varchar(n) can store up to n characters in length. If you try to store a longer string in the column that is either char(n) or varchar(n), PostgreSQL will issue an error.

However, one exception is that if the excess characters are all spaces, PostgreSQL will truncate the spaces to the maximum length and store the string.

If a string casts to a char(n) or varchar(n) explicitly, PostgresQL will truncate the string to n characters before inserting into the table.

The text data type can store a string with unlimited length.

If you do not specify the n integer for the varchar data type, it behaves like the text data type. The performance of the varchar (without n) and text are the same.

The only advantage of specifying the length specifier for the varchar data type is that PostgreSQL will check and issue an error if you try to insert a longer string into the varchar(n) column.

Unlike varchar, The character or  char without the length specifier is the same as the character(1) or char(1).

Different from other database systems, in PostgreSQL, there is no performance difference among three character types. In most situation, you should use text or varchar, and varchar(n) if you want PostgreSQL to check for the length limit.

PostgreSQL character type example

Let’s take a look at an example to see how the char, varchar, and text data types work.

First, we create a new table for the demonstration.

1
2
3
4
5
6
CREATE TABLE character_tests (
id serial PRIMARY KEY,
x CHAR (1),
y VARCHAR (10),
        z TEXT
);

Then, we insert a new row into the character_tests table.

1
2
3
4
5
6
7
INSERT INTO character_tests (x, y, z)
VALUES
(
'Yes',
'This is a test for varchar',
'This is a very long text for the PostgreSQL text column'
);

PostgreSQL issued an error:

1
ERROR:  value too long for type character(1)

This is because the data type of the x column is char(1) and we tried to insert a string with three characters into this column. Let’s fix it.

1
2
3
4
5
6
7
INSERT INTO character_tests (x, y, z)
VALUES
(
'Y',
'This is a test for varchar',
'This is a very long text for the PostgreSQL text column'
);

Now we got a different error.

1
ERROR:  value too long for type character varying(10)

This is because we tried to insert a string with more than 10 characters into the y column with the varchar(10) data type.

The following statement inserts a new row into the character_tests table successfully.

1
2
3
4
5
6
7
INSERT INTO character_tests (x, y, z)
VALUES
(
'Y',
'varchar(n)',
'This is a very long text for the PostgreSQL text column'
);

1
SELECT * FROM character_tests;

1
2
3
4
id | x |     y      |                            z
----+---+------------+---------------------------------------------------------
  1 | Y | varchar(n) | This is a very long text for the PostgreSQL text column
(1 row)

Now you should know how to choose the right character data type for your database table. Most of the time, you should choose text or varchar without length specifier.

Related Tutorials

  • PostgreSQL Data Types
Previous Tutorial: PostgreSQL Boolean Data Type with Practical Examples
Next Tutorial: The Ultimate Guide to PostgreSQL Date By Examples

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.