Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Drizzle ORM · Function

pgEnum

pgEnum defines a reusable PostgreSQL enum from a type name and allowed string values. The resulting builder can be used as a table column type and as input to Drizzle's schema-generation integrations.

How it works

pgEnum creates a reusable PostgreSQL enum definition from a name and a literal list of allowed values. Calling the returned enum builder for a column applies that database enum type while preserving the value union in TypeScript.

Syntax

pgEnum(name, values)

Parameters

NameTypeDescription
namestringThe PostgreSQL enum type name.
valuesstring[]The permitted enum string values.

Returns

A reusable PostgreSQL enum column builder.

Key points

  • The helper accepts a type name followed by an array of allowed values.
  • A named enum definition can be reused wherever a PostgreSQL column needs that enum type.
  • Drizzle's Zod integration supports enum schemas in addition to table and view schemas.
  • createSelectSchema can generate a validation schema directly from a Drizzle enum definition.

Common mistakes

  • The enum values are declared explicitly in the second argument; adding or removing values requires changing the schema definition and handling the resulting migration.

Example: Define and use a PostgreSQL enum

import { pgEnum, pgTable, serial } from 'drizzle-orm/pg-core';

export const role = pgEnum('role', ['admin', 'basic']);

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  role: role('role').notNull(),
});

Defines a PostgreSQL enum named role with two permitted values, then uses it as a column type in the users table.

When to use it

Model constrained database values

Represent a fixed set of PostgreSQL values such as user roles or workflow states.

Share a schema enum

Reuse one enum definition across multiple table columns.

Derive runtime validation

Generate validation for enum selections with Drizzle's Zod integration.

Interview questions

QWhat does pgEnum do, and how is it used?

pgEnum is imported from drizzle-orm/pg-core and takes an enum name plus an array of allowed string values. The resulting builder can be called when declaring a PostgreSQL table column.

Follow-up: How can the enum be used with generated validation? The documentation shows passing the enum to createSelectSchema, which produces a schema that parses the declared values.

QWhat are the arguments to pgEnum?

The first argument supplies the PostgreSQL enum type name, while the second lists its permitted values. For example, pgEnum('roles', ['admin', 'basic']) creates a reusable enum definition.

Follow-up: How would you scope an enum to a PostgreSQL schema? Use the schema-specific enum declaration shown in the schemas documentation rather than the root pgEnum helper.

QHow does pgEnum work with validation schemas?

A pgEnum definition can be consumed by schema-generation integrations. The official example passes roles to createSelectSchema and parses a value constrained to the enum's declared members.

Follow-up: What additional benefit does this provide? It lets validation schemas reflect the Drizzle enum definition instead of duplicating the permitted values manually.

  • pgTable
  • pgSchema
  • createSelectSchema
  • pgView

Source: zod - Drizzle ORM. Checked 2026-10-16.