Quick start

Lets build a quick start app with PostgreSQL + postgresjs and run our first migration.

The first thing we need to do is to install drizzle-orm and drizzle-kit:

npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit

Lets declare our schema.ts:

📦 <project root>
 ├ ...
 ├ 📂 src
 │ └ 📜 schema.ts
 └ 📜 package.json
schema.ts
import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core";

export const user = pgTable("user", {
  id: serial("id"),
  name: text("name"),
  email: text("email"),
  password: text("password"),
  role: text("role").$type<"admin" | "customer">(),
  createdAt: timestamp("created_at"),
  updatedAt: timestamp("updated_at"),
});

Now lets add drizzle configuration file:

📦 <project root>
 ├ ...
 ├ 📂 src
 ├ 📜 drizzle.config.ts
 └ 📜 package.json
import { defineConfig } from "drizzle-kit";

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

Add generate and migrate commands to package.json and run our first migrations generation:

package.json
{
  "name": "first time?",
  "version": "0.0.1",
  "scripts": {
    "generate": "drizzle-kit generate",
    "migrate": "drizzle-kit migrate"
  }, 
}
terminal
$ npm run generate
...

[✓] Your SQL migration file ➜ drizzle/0000_pale_mister_fear.sql 🚀

Done! We now have our first SQL migration file 🥳

📦 <project root>
 ├ 📂 drizzle
 │ ├ 📂 _meta
 │ └ 📜 0000_pale_mister_fear.sql
 ├ 📂 src
 ├ 📜 drizzle.config.ts
 └ 📜 package.json

Now lets run our first migration to the database:

terminal
$ npm run migrate

That’s it, folks!

My personal congratulations 🎉