ALTER_TYPE(7pg17)
πΈ NAME πΈ
hey bestie π welcome to ALTER_TYPE(7pg17), straight from the PostgreSQL 17.11 docs β the manual page for changing the definition of a type. Yes, thatβs the whole vibe: weβre editing type definitions and doing it fabulously.
ALTER_TYPE β change the definition of a type.
π¦ SYNOPSIS π¦
okay so these are the shapes this command can take, and yes, every line matters:
ALTER TYPE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER TYPE name RENAME TO new_name
ALTER TYPE name SET SCHEMA new_schema
ALTER TYPE name RENAME ATTRIBUTE attribute_name TO new_attribute_name [ CASCADE | RESTRICT ]
ALTER TYPE name action [, ... ]
ALTER TYPE name ADD VALUE [ IF NOT EXISTS ] new_enum_value [ { BEFORE | AFTER } neighbor_enum_value ]
ALTER TYPE name RENAME VALUE existing_enum_value TO new_enum_value
ALTER TYPE name SET ( property = value [, ... ] )
where action is one of:
ADD ATTRIBUTE attribute_name data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
DROP ATTRIBUTE [ IF EXISTS ] attribute_name [ CASCADE | RESTRICT ]
ALTER ATTRIBUTE attribute_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
π DESCRIPTION π
ALTER TYPE changes the definition of an existing type. There are several subforms, and honestly each one is serving a different little purpose:
π OWNER
This form changes the owner of the type. New owner, new era, etc. π
π RENAME
This form changes the name of the type. iconic behavior.
π SET SCHEMA
This form moves the type into another schema. packing up and relocating, type edition.
π¦ RENAME ATTRIBUTE
This form is only usable with composite types. It changes the name of an individual attribute of the type. one little attribute gets a whole glow-up.
β¨ ADD ATTRIBUTE
This form adds a new attribute to a composite type, using the same syntax as CREATE TYPE.
π DROP ATTRIBUTE [ IF EXISTS ]
This form drops an attribute from a composite type. If IF EXISTS is specified and the attribute does not exist, no error is thrown. In this case a notice is issued instead. so itβs like βgirl, sheβs already gone, but okayβ β no error, just a notice.
π ALTER ATTRIBUTE ... SET DATA TYPE
This form changes the type of an attribute of a composite type.
π ADD VALUE [ IF NOT EXISTS ] [ BEFORE | AFTER ]
This form adds a new value to an enum type. The new valueβs place in the enumβs ordering can be specified as being BEFORE or AFTER one of the existing values. Otherwise, the new item is added at the end of the list of values.
If IF NOT EXISTS is specified, it is not an error if the type already contains the new value: a notice is issued but no other action is taken. Otherwise, an error will occur if the new value is already present. so IF NOT EXISTS is the βno dramaβ switch π
πΈ RENAME VALUE
This form renames a value of an enum type. The valueβs place in the enumβs ordering is not affected. An error will occur if the specified value is not present or the new name is already present.
π§ SET ( property = value [, ... ] )
This form is only applicable to base types. It allows adjustment of a subset of the base-type properties that can be set in CREATE TYPE. Specifically, these properties can be changed:
RECEIVEβ can be set to the name of a binary input function, orNONEto remove the typeβs binary input function. Using this option requires superuser privilege.SENDβ can be set to the name of a binary output function, orNONEto remove the typeβs binary output function. Using this option requires superuser privilege.TYPMOD_INβ can be set to the name of a type modifier input function, orNONEto remove the typeβs type modifier input function. Using this option requires superuser privilege.TYPMOD_OUTβ can be set to the name of a type modifier output function, orNONEto remove the typeβs type modifier output function. Using this option requires superuser privilege.ANALYZEβ can be set to the name of a type-specific statistics collection function, orNONEto remove the typeβs statistics collection function. Using this option requires superuser privilege.SUBSCRIPTβ can be set to the name of a type-specific subscripting handler function, orNONEto remove the typeβs subscripting handler function. Using this option requires superuser privilege.STORAGEβ can be set toplain,extended,external, ormain(see Section 65.2 for more information about what these mean). However, changing fromplainto another setting requires superuser privilege (because it requires that the typeβs C functions all be TOAST-ready), and changing toplainfrom another setting is not allowed at all (since the type may already have TOASTed values present in the database). Note that changing this option doesnβt by itself change any stored data, it just sets the default TOAST strategy to be used for table columns created in the future. SeeALTER TABLE(ALTER_TABLE(7)) to change the TOAST strategy for existing table columns.
See CREATE TYPE (CREATE_TYPE(7)) for more details about these type properties. Note that where appropriate, a change in these properties for a base type will be propagated automatically to domains based on that type.
The ADD ATTRIBUTE, DROP ATTRIBUTE, and ALTER ATTRIBUTE actions can be combined into a list of multiple alterations to apply in parallel. For example, it is possible to add several attributes and/or alter the type of several attributes in a single command. multi-tasking queen π
You must own the type to use ALTER TYPE. To change the schema of a type, you must also have CREATE privilege on the new schema. To alter the owner, you must be able to SET ROLE to the new owning role, and that role must have CREATE privilege on the typeβs schema. (These restrictions enforce that altering the owner doesnβt do anything you couldnβt do by dropping and recreating the type. However, a superuser can alter ownership of any type anyway.) To add an attribute or alter an attribute type, you must also have USAGE privilege on the attributeβs data type. permissions check, bestie β no sneaking in π«Ά
π PARAMETERS π
nameβ The name (possibly schema-qualified) of an existing type to alter.new_nameβ The new name for the type.new_ownerβ The user name of the new owner of the type.new_schemaβ The new schema for the type.attribute_nameβ The name of the attribute to add, alter, or drop.new_attribute_nameβ The new name of the attribute to be renamed.data_typeβ The data type of the attribute to add, or the new type of the attribute to alter.new_enum_valueβ The new value to be added to an enum typeβs list of values, or the new name to be given to an existing value. Like all enum literals, it needs to be quoted.neighbor_enum_valueβ The existing enum value that the new value should be added immediately before or after in the enum typeβs sort ordering. Like all enum literals, it needs to be quoted.existing_enum_valueβ The existing enum value that should be renamed. Like all enum literals, it needs to be quoted.propertyβ The name of a base-type property to be modified; see above for possible values.CASCADEβ Automatically propagate the operation to typed tables of the type being altered, and their descendants.RESTRICTβ Refuse the operation if the type being altered is the type of a typed table. This is the default.
π NOTES π
π¨ transaction tea: if ALTER TYPE ... ADD VALUE (the form that adds a new value to an enum type) is executed inside a transaction block, the new value cannot be used until after the transaction has been committed. periodt.
Comparisons involving an added enum value will sometimes be slower than comparisons involving only original members of the enum type. This will usually only occur if BEFORE or AFTER is used to set the new valueβs sort position somewhere other than at the end of the list. However, sometimes it will happen even though the new value is added at the end (this occurs if the OID counter βwrapped aroundβ since the original creation of the enum type). The slowdown is usually insignificant; but if it matters, optimal performance can be regained by dropping and recreating the enum type, or by dumping and restoring the database.
π EXAMPLES π
To rename a data type:
ALTER TYPE electronic_mail RENAME TO email;
To change the owner of the type email to joe:
ALTER TYPE email OWNER TO joe;
To change the schema of the type email to customers:
ALTER TYPE email SET SCHEMA customers;
To add a new attribute to a composite type:
ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
To add a new value to an enum type in a particular sort position:
ALTER TYPE colors ADD VALUE 'orange' AFTER 'red';
To rename an enum value:
ALTER TYPE colors RENAME VALUE 'purple' TO 'mauve';
To create binary I/O functions for an existing base type:
CREATE FUNCTION mytypesend(mytype) RETURNS bytea ...;
CREATE FUNCTION mytyperecv(internal, oid, integer) RETURNS mytype ...;
ALTER TYPE mytype SET (
SEND = mytypesend,
RECEIVE = mytyperecv
);
π COMPATIBILITY π
The variants to add and drop attributes are part of the SQL standard; the other variants are PostgreSQL extensions. so some of this is standard, some of this is PostgreSQL being extra β and we love that for her π
π₯ SEE ALSO π₯
CREATE TYPE (CREATE_TYPE(7)), DROP TYPE (DROP_TYPE(7))
thatβs the whole page, bestie π β source/version: PostgreSQL 17.11, dated 2026. mwah π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms