CREATE TYPE  --  Defines a new data type for use in the database.


CREATE TYPE typename ( INPUT = input_function, OUTPUT = output_function
      , INTERNALLENGTH = { internallength | VARIABLE } 
    [ , EXTERNALLENGTH = { externallength | VARIABLE } ]
    [ , DEFAULT = "default" ]
    [ , ELEMENT = element ] [ , DELIMITER = delimiter ]
    [ , SEND = send_function ] [ , RECEIVE = receive_function ]
    [ , ALIGNMENT =alignment ] 
    [ , STORAGE =storage ] )



Use this parameter to specify the name of the new type being created.


Use this parameter to set the internal length of the new type.


Use this parameter to set the external (displayed) length of the new type.


Use this parameter to specify the name of the new type's input function. You must have already defined the function with CREATE FUNCTION and it must act to convert data of the type's external form into the type's internal form.


Use this parameter to specify the name of the new type's output function. This function must convert data of the type's internal form into a displayable form.


If the type is an array, use this parameter to identify the type of the array elements.


Use this parameter to specify the array element delimiter.


Use this parameter to specify what the default value for this type should be. Normally this is NULL if not specified.


Use this parameter to specify the name of the new type's send function. This function should convert data of this type into a form that can be transfered to another machine.


Use this parameter to specify the name of the new type's receive function. This function should accept data of the form outputted by the send_function and convert it into the type internal form.


Use this parameter to set the storage alignment this type will require. This must be either 'int4' or 'double.' If left unspecified, 'int4' will be chosen by default.


Use this parameter to specify the storage technique that will be used for this type. Set this to one of 'plain,' 'external,' 'extended,' or 'main.' If this is left unspecified, the storage type will default to plain.



This message is displayed if the type is successfully created.


Use the CREATE TYPE command to register a new user-defined data type with the current database. The user account that issues the command becomes the owner of the data type if creation is successful. All data types within a PostgreSQL database must have unique names, therefor you must choose a unique name for the new data type.

For a type to be created it must use two pre-existing functions (pre-existing in the sense that you have already used CREATE FUNCTION to register them); these functions are the input function and the output function. The input function converts the type's external representation into an internal representation that can be used by the system objects associated with the type. The output function converts the internal representation back to an external representation. They must both take either one or two arguments of the opaque type.

You can set the type as either fixed or variable length. If fixed-length, use internallength to set its length. If variable-length, use 'VARIABLE' for the internallength parameter and the length will be handled in the same way as the text data type. Specify the external length in the same way, using the externallength keyword.

Use the ELEMENT keyword to specify the type is an array. If you wish to provide a delimiter character for the array type, use delimiter to do so. By default this is set to a comma.

To define a default value, use the DEFAULT keyword.


This example demonstrates the creation of an int4 array type.

booktown=# CREATE TYPE intarray (INPUT = array_in, OUTPUT = array_out,
booktown(#     INTERNALLENGTH = VARIABLE, ELEMENT = int4);