Skip to main content
Neon is a fully managed serverless Postgres, separating compute and storage to offer features like autoscaling, branching and bottomless storage. You can use Neon from Bun directly with the @neondatabase/serverless driver or through an ORM like Drizzle. Drizzle ORM supports both a SQL-like “query builder” API and an ORM-like Queries API. Get started by creating a project directory, initializing it with bun init, and installing Drizzle and the Neon serverless driver.
terminal

Create a .env.local file and add your Neon Postgres connection string to it.
.env.local

In db.ts, connect to the Neon database with the Neon serverless driver, wrapped in a Drizzle database instance.
db.ts

To see the database in action, add these lines to index.ts.
index.ts

Then run index.ts with Bun.
terminal

Define a schema for the database with Drizzle ORM primitives. Create a schema.ts file and add this code.
schema.ts

Then use the drizzle-kit CLI to generate an initial SQL migration.

The command creates a drizzle directory containing a .sql migration file and a meta directory.
File Tree

Execute these migrations with a migrate.ts script. The script opens a new connection to the Neon database and executes all unexecuted migrations in the drizzle directory.
migrate.ts

Run the script with bun.
terminal

Now add some data to the database. Create a seed.ts file with the following contents.
seed.ts

Then run this file.
terminal

The database now has a schema and sample data. Query it with Drizzle by replacing the contents of index.ts with the following.
index.ts

Then run the file. It prints the three authors you inserted.
terminal

This example used the Neon serverless driver’s SQL-over-HTTP functionality. Neon’s serverless driver also exposes Client and Pool constructors to enable sessions, interactive transactions, and node-postgres compatibility. Refer to Neon’s documentation for a complete overview. Refer to the Drizzle website for complete documentation.