Add a PostgreSQL database with Drizzle ORM to a Scaffold-ETH 2 project. Use when the user wants to: add a database, use Drizzle ORM, integrate Neon PostgreSQL, store off-chain data, build a backend with database, or add persistent storage to their dApp.
Check if ./packages/nextjs/scaffold.config.ts exists directly in the current working directory (do not search subdirectories). If it doesn't exist, this is not a Scaffold-ETH 2 project. Follow the instructions at https://docs.scaffoldeth.io/SKILL.md to scaffold it first. If it exists, continue directly with building.
Drizzle ORM is a TypeScript ORM for SQL databases with a type-safe query builder. Neon is a serverless PostgreSQL platform. This skill integrates both into SE-2, with a smart database client that auto-detects the environment (local Postgres via Docker, Neon serverless, or Neon HTTP) and uses the optimal driver.
For Drizzle API reference beyond what's covered here, refer to the Drizzle docs. For Neon specifics, see the Neon docs. This skill focuses on SE-2 integration patterns and the tri-driver architecture.
Add to packages/nextjs/package.json:
{
"scripts": {
"db:seed": "tsx services/database/seed.ts",
"db:wipe": "tsx services/database/wipe.ts",
"drizzle-kit": "drizzle-kit"
},
"dependencies": {
"@neondatabase/serverless": "^1.0.0",
"dotenv": "^17.0.0",
"drizzle-orm": "^0.44.0",
"pg": "^8.16.0"
},
"devDependencies": {
"@types/pg": "^8",
"drizzle-kit": "^0.31.0",
"drizzle-seed": "^0.3.0",
"tsx": "^4.20.0"
}
}
{
"drizzle-kit": "yarn workspace @se-2/nextjs drizzle-kit",
"db:seed": "yarn workspace @se-2/nextjs db:seed",
"db:wipe": "yarn workspace @se-2/nextjs db:wipe"
}
Create packages/nextjs/.env.development:
POSTGRES_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres"
Also add POSTGRES_URL= to packages/nextjs/.env.example.
Create docker-compose.yml at project root: