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

Drizzle ORM · Function

varchar

Drizzle ORM's PostgreSQL varchar builder declares variable-length character columns. Configure an optional character limit or an enum array for narrower inferred TypeScript insert and select types.

How it works

varchar creates a PostgreSQL variable-length character column. You may omit its configuration, set a maximum character length with length, or provide enum values so Drizzle infers the insert and select types; enum configuration does not validate runtime values.

Syntax

varchar(config?)

Parameters

NameTypeDescription
config{ length?: number; enum?: string[] }Optional configuration object; supports length for the character limit and enum for inferred insert/select string values.

Returns

A PostgreSQL varchar column builder.

Key points

  • PostgreSQL varchar stores variable-length strings, and a specified limit counts characters rather than bytes.
  • Use the PostgreSQL builder by importing varchar and pgTable from drizzle-orm/pg-core.
  • Pass a numeric length in the configuration object to create a bounded varchar column.
  • The enum configuration narrows Drizzle's inferred insert and select types to the listed string values.

Common mistakes

  • The enum option affects TypeScript inference only; it does not enforce the allowed values at runtime.
  • A length is optional, so an unbounded PostgreSQL varchar can be declared with no arguments.

Example: Define PostgreSQL varchar columns

import { pgTable, varchar } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
  name: varchar({ length: 256 }).notNull(),
  role: varchar({ enum: ["admin", "member"] }),
});

Declares a PostgreSQL table with a length-limited required name and an enum-inferred role column.

When to use it

Store variable-length names and email addresses

User profile fields

Limit values such as labels or short descriptions with `length`

Bounded textual attributes

Infer a string union for known categories with `enum`

Type-narrowed categorical strings

Interview questions

QWhat does Drizzle's PostgreSQL varchar builder represent?

varchar defines a variable-length character string column in PostgreSQL. Its optional length setting limits the stored string to that many characters.

Follow-up: How can the schema infer a literal union for the column? Use enum: ["admin", "member"]; remember this does not add runtime validation.

QHow do you configure the maximum length?

Call varchar() for an unrestricted column or varchar({ length: 256 }) for a length-limited one. The builder is imported from drizzle-orm/pg-core.

Follow-up: What SQL does the length-limited form correspond to? It produces a PostgreSQL varchar(256) column declaration.

QWhat is the purpose and limitation of the enum option?

Pass an enum array in the configuration, such as varchar({ enum: ["admin", "member"] }). Drizzle uses it to infer insert and select types.

Follow-up: Does this protect a database from arbitrary strings? No; the documentation explicitly says the enum configuration does not check runtime values.

  • pgTable
  • text
  • char
  • notNull
  • unique

Source: timestamp. Checked 2026-10-16.