Become a Gold Sponsor

drizzle-kit export

This guide assumes familiarity with:

drizzle-kit export lets you export SQL representation of Drizzle schema and print in console SQL DDL representation on it.

How it works under the hood?

Drizzle Kit export command triggers a sequence of events:

  1. It will read through your Drizzle schema file(s) and compose a json snapshot of your schema
  2. Based on json differences it will generate SQL DDL statements
  3. Output SQL DDL statements to console

It’s designed to cover codebase first approach of managing Drizzle migrations. You can export the SQL representation of the Drizzle schema, allowing external tools like Atlas to handle all the migrations for you

drizzle-kit export command requires you to provide both dialect and schema path options, you can set them either via drizzle.config.ts config file or via CLI options

With config file
As CLI options
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
});
npx drizzle-kit export

Schema files path

You can have a single schema.ts file or as many schema files as you want spread out across the project. Drizzle Kit requires you to specify path(s) to them as a glob via schema configuration option.

Example 1
Example 2
Example 3
Example 4
πŸ“¦ <project root>
 β”œ ...
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”‚ β”œ ...
 β”‚ β”œ πŸ“œ index.ts
 β”‚ β”” πŸ“œ schema.ts 
 β”œ πŸ“œ drizzle.config.ts
 β”” πŸ“œ package.json
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./src/schema.ts",
});

Multiple configuration files in one project

You can have multiple config files in the project, it’s very useful when you have multiple database stages or multiple databases or different databases on the same project:

npm
yarn
pnpm
bun
npx drizzle-kit export --config=drizzle-dev.config.ts
npx drizzle-kit export --config=drizzle-prod.config.ts
πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”œ πŸ“œ .env
 β”œ πŸ“œ drizzle-dev.config.ts
 β”œ πŸ“œ drizzle-prod.config.ts
 β”œ πŸ“œ package.json
 β”” πŸ“œ tsconfig.json

Extended list of available configurations

drizzle-kit export has a list of cli-only options

--sqlgenerating SQL representation of Drizzle Schema

By default, Drizzle Kit outputs SQL files, but in the future, we want to support different formats

npm
yarn
pnpm
bun
npx drizzle-kit push --name=init
npx drizzle-kit push --name=seed_users --custom



We recommend configuring drizzle-kit through drizzle.config.ts file, yet you can provide all configuration options through CLI if necessary, e.g. in CI/CD pipelines, etc.

dialectrequiredDatabase dialect, one of postgresql mysql sqlite turso singlestore
schemarequiredPath to typescript schema file(s) or folder(s) with multiple schema files
configConfiguration file path, default is drizzle.config.ts

Example

Example of how to export drizzle schema to console with Drizzle schema located in ./src/schema.ts

We will also place drizzle config file in the configs folder.

Let’s create config file:

πŸ“¦ <project root>
 β”œ πŸ“‚ configs
 β”‚ β”” πŸ“œ drizzle.config.ts
 β”œ πŸ“‚ src
 β”‚ β”” πŸ“œ schema.ts
 β”” …
drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
});
schema.ts
import { pgTable, serial, text } from 'drizzle-orm/pg-core'

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

Now let’s run

npx drizzle-kit export --config=./configs/drizzle.config.ts

And it will successfully output SQL representation of drizzle schema

CREATE TABLE "users" (
        "id" serial PRIMARY KEY NOT NULL,
        "email" text NOT NULL,
        "name" text
);