ALTER_TYPE(7)
โจ ALTER TYPE โ bestie edition ๐
okay so this is the PostgreSQL 18.6 Documentation page for ALTER TYPE, and it's literally about changing the definition of an existing type. it's giving โจmakeover episodeโจ but for your database schema. no cap.
๐ธ NAME ๐ธ
ALTER_TYPE โ change the definition of a type ๐ฆ
๐ SYNOPSIS ๐
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 a little main character:
๐ OWNER
this form changes the owner of the type. passing the aux, basically.
๐ RENAME
this form changes the name of the type. glow-up via new government name โจ
๐ SET SCHEMA
this form moves the type into another schema. it's moving out, it's getting its own apartment ๐
๐ RENAME ATTRIBUTE
this form is only usable with composite types. it changes the name of an individual attribute of the type.
๐ 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, "bestie i looked, she's not here, but no drama ๐
")
๐ 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.
๐ 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:
RECEIVEcan be set to the name of a binary input function, orNONEto remove the type's binary input function. using this option requires superuser privilege.SENDcan 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_INcan 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_OUTcan 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.ANALYZEcan 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.SUBSCRIPTcan 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.STORAGEcan be set toplain,extended,external, ormain(see Section 66.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. it's giving generational wealth ๐ธ
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. efficiency 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.
๐ฆ 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 ๐
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. like girl, she's not available yet, she's still getting ready ๐ชโจ
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. sometimes the only fix is a hard reset bestie ๐ญ
๐ 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. iykyk ๐
๐ง SEE ALSO ๐ง
CREATE TYPE(CREATE_TYPE(7))DROP TYPE(DROP_TYPE(7))
that's the whole page bestie, you ate that ๐ซถ this was PostgreSQL 18.6, documented 2026, and it's iconic fr fr. go alter something (responsibly, and with the right privileges) ๐โจ๐๐ฆ๐ธ๐
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms