Home Explore Blog CI



postgresql

14th chunk of `doc/src/sgml/datatype.sgml`
bb047c6d6368d9c24262f5aada8718f4eb13e04052d88c140000000100000fa4
 money); the currency units cancel each other out in the division.
   </para>
  </sect1>


  <sect1 id="datatype-character">
   <title>Character Types</title>

   <indexterm zone="datatype-character">
    <primary>character string</primary>
    <secondary>data types</secondary>
   </indexterm>

   <indexterm>
    <primary>string</primary>
    <see>character string</see>
   </indexterm>

   <indexterm zone="datatype-character">
    <primary>character</primary>
   </indexterm>

   <indexterm zone="datatype-character">
    <primary>character varying</primary>
   </indexterm>

   <indexterm zone="datatype-character">
    <primary>text</primary>
   </indexterm>

   <indexterm zone="datatype-character">
    <primary>char</primary>
   </indexterm>

   <indexterm zone="datatype-character">
    <primary>varchar</primary>
   </indexterm>

   <indexterm zone="datatype-character">
    <primary>bpchar</primary>
   </indexterm>

    <table id="datatype-character-table">
     <title>Character Types</title>
     <tgroup cols="2">
      <thead>
       <row>
        <entry>Name</entry>
        <entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
        <entry><type>character varying(<replaceable>n</replaceable>)</type>, <type>varchar(<replaceable>n</replaceable>)</type></entry>
        <entry>variable-length with limit</entry>
       </row>
       <row>
        <entry><type>character(<replaceable>n</replaceable>)</type>, <type>char(<replaceable>n</replaceable>)</type>, <type>bpchar(<replaceable>n</replaceable>)</type></entry>
        <entry>fixed-length, blank-padded</entry>
       </row>
       <row>
        <entry><type>bpchar</type></entry>
        <entry>variable unlimited length, blank-trimmed</entry>
       </row>
       <row>
        <entry><type>text</type></entry>
        <entry>variable unlimited length</entry>
       </row>
     </tbody>
     </tgroup>
    </table>

   <para>
    <xref linkend="datatype-character-table"/> shows the
    general-purpose character types available in
    <productname>PostgreSQL</productname>.
   </para>

   <para>
    <acronym>SQL</acronym> defines two primary character types:
    <type>character varying(<replaceable>n</replaceable>)</type> and
    <type>character(<replaceable>n</replaceable>)</type>, where <replaceable>n</replaceable>
    is a positive integer.  Both of these types can store strings up to
    <replaceable>n</replaceable> characters (not bytes) in length.  An attempt to store a
    longer string into a column of these types will result in an
    error, unless the excess characters are all spaces, in which case
    the string will be truncated to the maximum length. (This somewhat
    bizarre exception is required by the <acronym>SQL</acronym>
    standard.)
    However, if one explicitly casts a value to <type>character
    varying(<replaceable>n</replaceable>)</type> or
    <type>character(<replaceable>n</replaceable>)</type>, then an over-length
    value will be truncated to <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

Title: Character Types in PostgreSQL
Summary
PostgreSQL offers various character types, including character varying, character, and text, each with its own characteristics, such as fixed or variable length, and space-padded or blank-trimmed. The SQL standard defines character varying and character types, while text is a native string data type in PostgreSQL that stores strings of any length. The database management system also supports other character types, such as varchar and bpchar, with specific traits and uses.