<replaceable class="parameter">name</replaceable>
</synopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
<command>CREATE TYPE</command> registers a new data type for use in
the current database. The user who defines a type becomes its
owner.
</para>
<para>
If a schema name is given then the type is created in the specified
schema. Otherwise it is created in the current schema. The type
name must be distinct from the name of any existing type or domain
in the same schema. (Because tables have associated data types,
the type name must also be distinct from the name of any existing
table in the same schema.)
</para>
<para>
There are five forms of <command>CREATE TYPE</command>, as shown in the
syntax synopsis above. They respectively create a <firstterm>composite
type</firstterm>, an <firstterm>enum type</firstterm>, a <firstterm>range type</firstterm>, a
<firstterm>base type</firstterm>, or a <firstterm>shell type</firstterm>. The first four
of these are discussed in turn below. A shell type is simply a placeholder
for a type to be defined later; it is created by issuing <command>CREATE
TYPE</command> with no parameters except for the type name. Shell types
are needed as forward references when creating range types and base types,
as discussed in those sections.
</para>
<refsect2>
<title>Composite Types</title>
<para>
The first form of <command>CREATE TYPE</command>
creates a composite type.
The composite type is specified by a list of attribute names and data types.
An attribute's collation can be specified too, if its data type is
collatable. A composite type is essentially the same as the row type
of a table, but using <command>CREATE TYPE</command> avoids the need to
create an actual table when all that is wanted is to define a type.
A stand-alone composite type is useful, for example, as the argument or
return type of a function.
</para>
<para>
To be able to create a composite type, you must
have <literal>USAGE</literal> privilege on all attribute types.
</para>
</refsect2>
<refsect2 id="sql-createtype-enum">
<title>Enumerated Types</title>
<para>
The second form of <command>CREATE TYPE</command> creates an enumerated
(enum) type, as described in <xref linkend="datatype-enum"/>.
Enum types take a list of quoted labels, each of which
must be less than <symbol>NAMEDATALEN</symbol> bytes long (64 bytes in a
standard <productname>PostgreSQL</productname> build). (It is possible to
create an enumerated type with zero labels, but such a type cannot be used
to hold values before at least one label is added using <link
linkend="sql-altertype"><command>ALTER TYPE</command></link>.)
</para>
</refsect2>
<refsect2 id="sql-createtype-range">
<title>Range Types</title>
<para>
The third form of <command>CREATE TYPE</command> creates a new
range type, as described in <xref linkend="rangetypes"/>.
</para>
<para>
The range type's <replaceable class="parameter">subtype</replaceable> can
be any type with an associated b-tree operator class (to determine the
ordering of values for the range type). Normally the subtype's default
b-tree operator class is used to determine ordering; to use a non-default
operator class, specify its name with <replaceable
class="parameter">subtype_opclass</replaceable>. If the subtype is
collatable, and you want to use a non-default collation in the range's
ordering, specify the desired collation with the <replaceable
class="parameter">collation</replaceable> option.
</para>
<para>
The optional <replaceable class="parameter">canonical</replaceable>
function must take one argument of the range type being defined, and
return a value of the same type. This is used to convert range values
to a canonical form, when applicable. See <xref