PostgreSQL TEXT Data Type - Features, Examples and Equivalents

TEXT data type stores variable-length character data.

Syntax TEXT
Quick Example CREATE TABLE t (c TEXT);
Range up to 1 Gb
Trailing Spaces Stored and retrieved if data contains them. Significant in comparison

Versions: PostgreSQL 9.x and 8.x

Related Data Types in PostgreSQL

Related data types for TEXT in PostgreSQL:

CHAR(n) Fixed-length 1 ⇐ n < 1 Gb Default is 1
VARCHAR(n) Variable-length 1 ⇐ n < 1 Gb Default is 1 Gb

Difference Between PostgreSQL TEXT and VARCHAR Data Types

The only difference between TEXT and VARCHAR(n) is that you can limit the maximum length of a VARCHAR column, for example, VARCHAR(255) does not allow inserting a string more than 255 characters long.

Both TEXT and VARCHAR have the upper limit at 1 Gb, and there is no performance difference among them (according to the PostgreSQL documentation).

PostgreSQL TEXT Data Type Details

In a value inserted to a TEXT column has trailing blanks, PostgreSQL does not truncate them, and they are significant in comparisons:

  CREATE TABLE t_text1 (c1 TEXT);
 
  INSERT INTO t_text1 VALUES ('A   ');
 
  SELECT '''' || c1 || '''' FROM t_text1;
  -- Result: 'A   '
 
  SELECT COUNT(c1) FROM t_text1 WHERE c1 = 'A';
  -- Result: 0
 
  SELECT COUNT(c1) FROM t_text1 WHERE c1 = 'A   ';
  -- Result: 1

PostgreSQL TEXT in Other Databases

Similar data types in other databases:

Oracle:

VARCHAR2(n) 1 ⇐ n ⇐ 4000/charsize
CLOB Up to 4 Gb Cannot be used in ORDER BY and GROUP BY

SQL Server:

VARCHAR(n | max) 1 ⇐ n ⇐ 8000 Up to 2 Gb if max is specified
TEXT Up to 2 Gb Legacy, not recommended for use

MySQL:

VARCHAR(n) 1 ⇐ n ⇐ 65535 Max row length is 65535 among on columns in a table
TEXT Up to 65535 bytes
MEDIUMTEXT Up to 16 Mb
LONGTEXT Up to 4 Gb

PostgreSQL TEXT Data Type Conversion to Other Databases

Convertion of TEXT data type:

PostgreSQL:

   CREATE TABLE t_text2
   (
     c1 TEXT
   );

Oracle:

Although Oracle CLOB data type can store up to 4 Gb, converting TEXT to CLOB may be not appropriate in some cases due to CLOB restrictions in Oracle:

  • In PostgreSQL, you can actually store relatively short strings in TEXT columns and then use them in GROUP BY and ORDER BY
  • In Oracle, you can use CLOB in string functions, but you cannot use them in GROUP BY and ORDER BY

TEXT to CLOB:

   CREATE TABLE t_text2
   (
     c1 CLOB
   );
 
   INSERT INTO t_text2 VALUES('ABC');
 
   -- Using CLOB in string functions
   SELECT SUBSTR(c1, 1, 1), LENGTH(c1) FROM t_text2;
   -- Result: A    3
 
   SELECT * FROM t_text2 ORDER BY c1;
   -- ERROR at line 1:
   -- ORA-00932: inconsistent datatypes: expected - got CLOB
 
   SELECT * FROM t_text2 GROUP BY c1;
   -- ERROR at line 1:
   -- ORA-00932: inconsistent datatypes: expected - got CLOB

If a TEXT column is used to store short strings and used in GROUP BY and ORDER BY, you can convert it to VARCHAR(4000) in Oracle:

   CREATE TABLE t_text2
   (
     c1 VARCHAR(4000)
   );

SQL Server:

You can convert TEXT to VARCHAR(max) in SQL Server that can store up to 2 Gb characters:

   CREATE TABLE t_text2
   (
     c1 VARCHAR(max)
   );

MySQL:

You can convert TEXT to LONGTEXT in MySQL that can store up to 4 Gb characters:

   CREATE TABLE t_text2
   (
     c1 LONGTEXT
   );

Convert Online

Resources