PostgreSQL: ENUM with a default value and a custom schema

Updated: January 4, 2024 By: Guest Contributor Post a comment

Introduction

Utilizing user-defined enumerations (ENUM) types in PostgreSQL can significantly enhance the clarity and integrity of your database models. This tutorial delves into the creation of ENUM types within a custom schema, complete with a default value set for hassle-free database operations.

Creating a Custom Schema

The concept of schemas in PostgreSQL is a way to encapsulate and organize database objects. It’s comparable to a namespace that can hold tables, types, functions, and more. Let’s start by creating a custom schema named my_schema:

CREATE SCHEMA my_schema;

This statement creates a new schema which will further be used to contain our custom ENUM type and other database objects we might want to define.

Defining a Custom ENUM Type

Enumeration (ENUM) types are a data type that comprises a static, ordered set of values. They are useful when you have a column with a value constrained to a particular list. Below is how to create an ENUM type in PostgreSQL:

CREATE TYPE my_schema.my_enum_type AS ENUM ('value1', 'value2', 'value3');

This enumeration defines the type my_enum_type with three possible values within our custom schema my_schema.

Setting a Default Value for an ENUM Column

To make datatypes more practical, PostgreSQL allows setting default values for columns. With ENUMs, we can conveniently set defaults that can streamline data insertion by autopopulating fields:

CREATE TABLE my_schema.my_table (
  id SERIAL PRIMARY KEY,
  status my_schema.my_enum_type NOT NULL DEFAULT 'value1'
);

This table creation command includes a column status which has a default value of ‘value1’ borrowed from our previously defined ENUM. This prevents the need for always specifying the status whenever a new record is inserted, as long as ‘value1’ is an acceptable value for the default use case.

Inserting Data into the ENUM Column

With the structure set, you can now insert data into the table using the custom ENUM type:

INSERT INTO my_schema.my_table (status) VALUES ('value2');

-- Since a default is set, this is also valid and will use 'value1' as the status:
INSERT INTO my_schema.my_table (status) VALUES (DEFAULT);

The first statement specifies a value for status while the second one uses DEFAULT, relying on the predefined default value for the ENUM column.

Advanced: Modifying ENUM Types and their Defaults

Once you have ENUMs in use, you might find the need for modifications due to changes in your application’s logic. Adding a new value to an ENUM type in PostgreSQL is done as follows:

ALTER TYPE my_schema.my_enum_type ADD VALUE 'value4';

Note that it’s not possible to remove a value directly from an ENUM type or rearrange existing values; such changes require more complex workarounds.

To change the default value of the ENUM column in a table, you’d use:

ALTER TABLE my_schema.my_table ALTER COLUMN status SET DEFAULT 'value2';

This alters the table such that any future insertions into the status column without a specific value will default to ‘value2’ instead of ‘value1’.

Working with Custom Schemas

Having your ENUMs in a custom schema might lead to additional considerations such as setting your search_path to ensure the objects within the schema can be referenced without a schema-qualified syntax:

SET search_path TO my_schema, public;

Now, you can reference the ENUM type as just my_enum_type and PostgreSQL knows to look in my_schema first before searching within public.

Maintaining Data Integrity and Performance

Using ENUMs can not only be more expressive compared to simple text representation but can also improve performance as ENUMs take up less space and comparing them can be faster than text equivalents. Always make sure that changes to ENUMs are compatible with your application logic and the real-world data scenarios you need to model.

Conclusion

PostgreSQL’s ENUM type with a default value in a custom schema is a potent feature, especially when combined with smart defaults and proper schema organization. Applied appropriately, they can provide a robust framework for ensuring data integrity while reflecting the natural categories present in your problem domain. Remember that although ENUMs can be edited after creation, it’s generally better to design them cautiously, avoiding the need for frequent changes.