Home Explore Blog CI



postgresql

15th chunk of `doc/src/sgml/datatype.sgml`
5073e64e8b8b271aeb914b9ba5f5ff33fd75277e3600325a0000000100000fa0
 <replaceable>n</replaceable> characters without
    raising an error. (This too is required by the
    <acronym>SQL</acronym> standard.)
    If the string to be stored is shorter than the declared
    length, values of type <type>character</type> will be space-padded;
    values of type <type>character varying</type> will simply store the
    shorter
    string.
   </para>

   <para>
    In addition, <productname>PostgreSQL</productname> provides the
    <type>text</type> type, which stores strings of any length.
    Although the <type>text</type> type is not in the
    <acronym>SQL</acronym> standard, several other SQL database
    management systems have it as well.
    <type>text</type> is <productname>PostgreSQL</productname>'s native
    string data type, in that most built-in functions operating on strings
    are declared to take or return <type>text</type> not <type>character
    varying</type>.  For many purposes, <type>character varying</type>
    acts as though it were a <link linkend="domains">domain</link>
    over <type>text</type>.
   </para>

   <para>
    The type name <type>varchar</type> is an alias for <type>character
    varying</type>, while <type>bpchar</type> (with length specifier) and
    <type>char</type> are aliases for <type>character</type>.  The
    <type>varchar</type> and <type>char</type> aliases are defined in the
    <acronym>SQL</acronym> standard;  <type>bpchar</type> is a
    <productname>PostgreSQL</productname> extension.
   </para>

   <para>
    If specified, the length <replaceable>n</replaceable> must be greater
    than zero and cannot exceed 10,485,760.  If <type>character
    varying</type> (or <type>varchar</type>) is used without
    length specifier, the type accepts strings of any length. If
    <type>bpchar</type> lacks a length specifier, it also accepts strings
    of any length, but trailing spaces are semantically insignificant.
    If <type>character</type> (or <type>char</type>) lacks a specifier,
    it is equivalent to <type>character(1)</type>.
   </para>

   <para>
    Values of type <type>character</type> are physically padded
    with spaces to the specified width <replaceable>n</replaceable>, and are
    stored and displayed that way.  However, trailing spaces are treated as
    semantically insignificant and disregarded when comparing two values
    of type <type>character</type>.  In collations where whitespace
    is significant, this behavior can produce unexpected results;
    for example <command>SELECT 'a '::CHAR(2) collate "C" &lt;
    E'a\n'::CHAR(2)</command> returns true, even though <literal>C</literal>
    locale would consider a space to be greater than a newline.
    Trailing spaces are removed when converting a <type>character</type> value
    to one of the other string types.  Note that trailing spaces
    <emphasis>are</emphasis> semantically significant in
    <type>character varying</type> and <type>text</type> values, and
    when using pattern matching, that is <literal>LIKE</literal> and
    regular expressions.
   </para>

   <para>
    The characters that can be stored in any of these data types are
    determined by the database character set, which is selected when
    the database is created.  Regardless of the specific character set,
    the character with code zero (sometimes called NUL) cannot be stored.
    For more information refer to <xref linkend="multibyte"/>.
   </para>

   <para>
    The storage requirement for a short string (up to 126 bytes) is 1 byte
    plus the actual string, which includes the space padding in the case of
    <type>character</type>.  Longer strings have 4 bytes of overhead instead
    of 1.  Long strings are compressed by the system automatically, so
    the physical requirement on disk might be less. Very long values are also
    stored in background tables so that they do not interfere with rapid
    access to shorter column values. In any case, the longest
    possible character string that can be

Title: Character Data Types in PostgreSQL
Summary
PostgreSQL provides various character data types, including character, character varying, text, varchar, and bpchar, each with its own characteristics, such as fixed or variable length, space-padded or blank-trimmed, and semantic significance of trailing spaces. The database character set determines the characters that can be stored, with the exception of the character with code zero. Storage requirements vary depending on the length of the string, with short strings requiring 1 byte of overhead and longer strings requiring 4 bytes, and may be compressed automatically by the system.