1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.7. Enumerated Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="datatype-boolean.html" title="8.6. Boolean Type" /><link rel="next" href="datatype-geometric.html" title="8.8. Geometric Types" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.7. Enumerated Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype-boolean.html" title="8.6. Boolean Type">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-geometric.html" title="8.8. Geometric Types">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-ENUM"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.7. Enumerated Types <a href="#DATATYPE-ENUM" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="datatype-enum.html#DATATYPE-ENUM-DECLARATION">8.7.1. Declaration of Enumerated Types</a></span></dt><dt><span class="sect2"><a href="datatype-enum.html#DATATYPE-ENUM-ORDERING">8.7.2. Ordering</a></span></dt><dt><span class="sect2"><a href="datatype-enum.html#DATATYPE-ENUM-TYPE-SAFETY">8.7.3. Type Safety</a></span></dt><dt><span class="sect2"><a href="datatype-enum.html#DATATYPE-ENUM-IMPLEMENTATION-DETAILS">8.7.4. Implementation Details</a></span></dt></dl></div><a id="id-1.5.7.15.2" class="indexterm"></a><a id="id-1.5.7.15.3" class="indexterm"></a><p>
3 Enumerated (enum) types are data types that
4 comprise a static, ordered set of values.
5 They are equivalent to the <code class="type">enum</code>
6 types supported in a number of programming languages. An example of an enum
7 type might be the days of the week, or a set of status values for
9 </p><div class="sect2" id="DATATYPE-ENUM-DECLARATION"><div class="titlepage"><div><div><h3 class="title">8.7.1. Declaration of Enumerated Types <a href="#DATATYPE-ENUM-DECLARATION" class="id_link">#</a></h3></div></div></div><p>
10 Enum types are created using the <a class="xref" href="sql-createtype.html" title="CREATE TYPE"><span class="refentrytitle">CREATE TYPE</span></a> command,
13 </p><pre class="programlisting">
14 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
17 Once created, the enum type can be used in table and function
18 definitions much like any other type:
19 </p><pre class="programlisting">
20 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
25 INSERT INTO person VALUES ('Moe', 'happy');
26 SELECT * FROM person WHERE current_mood = 'happy';
32 </p></div><div class="sect2" id="DATATYPE-ENUM-ORDERING"><div class="titlepage"><div><div><h3 class="title">8.7.2. Ordering <a href="#DATATYPE-ENUM-ORDERING" class="id_link">#</a></h3></div></div></div><p>
33 The ordering of the values in an enum type is the
34 order in which the values were listed when the type was created.
35 All standard comparison operators and related
36 aggregate functions are supported for enums. For example:
38 </p><pre class="programlisting">
39 INSERT INTO person VALUES ('Larry', 'sad');
40 INSERT INTO person VALUES ('Curly', 'ok');
41 SELECT * FROM person WHERE current_mood > 'sad';
43 -------+--------------
48 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
50 -------+--------------
57 WHERE current_mood = (SELECT MIN(current_mood) FROM person);
63 </p></div><div class="sect2" id="DATATYPE-ENUM-TYPE-SAFETY"><div class="titlepage"><div><div><h3 class="title">8.7.3. Type Safety <a href="#DATATYPE-ENUM-TYPE-SAFETY" class="id_link">#</a></h3></div></div></div><p>
64 Each enumerated data type is separate and cannot
65 be compared with other enumerated types. See this example:
67 </p><pre class="programlisting">
68 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
69 CREATE TABLE holidays (
73 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
74 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
75 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
76 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
77 ERROR: invalid input value for enum happiness: "sad"
78 SELECT person.name, holidays.num_weeks FROM person, holidays
79 WHERE person.current_mood = holidays.happiness;
80 ERROR: operator does not exist: mood = happiness
83 If you really need to do something like that, you can either
84 write a custom operator or add explicit casts to your query:
86 </p><pre class="programlisting">
87 SELECT person.name, holidays.num_weeks FROM person, holidays
88 WHERE person.current_mood::text = holidays.happiness::text;
95 </p></div><div class="sect2" id="DATATYPE-ENUM-IMPLEMENTATION-DETAILS"><div class="titlepage"><div><div><h3 class="title">8.7.4. Implementation Details <a href="#DATATYPE-ENUM-IMPLEMENTATION-DETAILS" class="id_link">#</a></h3></div></div></div><p>
96 Enum labels are case sensitive, so
97 <code class="type">'happy'</code> is not the same as <code class="type">'HAPPY'</code>.
98 White space in the labels is significant too.
100 Although enum types are primarily intended for static sets of values,
101 there is support for adding new values to an existing enum type, and for
102 renaming values (see <a class="xref" href="sql-altertype.html" title="ALTER TYPE"><span class="refentrytitle">ALTER TYPE</span></a>). Existing values
103 cannot be removed from an enum type, nor can the sort ordering of such
104 values be changed, short of dropping and re-creating the enum type.
106 An enum value occupies four bytes on disk. The length of an enum
107 value's textual label is limited by the <code class="symbol">NAMEDATALEN</code>
108 setting compiled into <span class="productname">PostgreSQL</span>; in standard
109 builds this means at most 63 bytes.
111 The translations from internal enum values to textual labels are
112 kept in the system catalog
113 <a class="link" href="catalog-pg-enum.html" title="52.20. pg_enum"><code class="structname">pg_enum</code></a>.
114 Querying this catalog directly can be useful.
115 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype-boolean.html" title="8.6. Boolean Type">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-geometric.html" title="8.8. Geometric Types">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.6. Boolean Type </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 8.8. Geometric Types</td></tr></table></div></body></html>