Movatterモバイル変換


[0]ホーム

URL:


Drizzle Studio Gateway is now FREE 👀
Documentation
30k+
Light
Dark
System
meet drizzle
Get started Why Drizzle? Guides Tutorials Latest releases Gotchas
Fundamentals
Schema Database connection Query data Migrations
Manage schema
Data types Indexes & Constraints Sequences Views Schemas Row-Level Security (RLS) Extensions Relations
Migrations
Overviewgeneratemigratepushpullexportcheckupstudio Custom migrations Migrations for teams Web and mobile drizzle.config.ts
Seeding
Overview Generators Versioning
Access your data
Query Select Insert Update Delete Filters Utils Joins Magic sql`` operator
Performance
Queries Serverless
Advanced
Set Operations Generated Columns Transactions Batch Cache Dynamic query building Read Replicas Custom types Goodies
Extensions
Prisma ESLint Plugin drizzle-zod drizzle-typebox drizzle-valibot drizzle-arktype drizzle-graphql
Become a Sponsor
Twitter
Discord
Product by Drizzle Team
One Dollar Stats$1 per mo web analytics
christmas
deal

Drizzle <> PostgreSQL

This guide assumes familiarity with:

Drizzle has native support for PostgreSQL connections with thenode-postgres andpostgres.js drivers.

There are a few differences between thenode-postgres andpostgres.js drivers that we discovered while using both and integrating them with the Drizzle ORM. For example:

node-postgres

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm pgnpm i -D drizzle-kit @types/pg
yarn add drizzle-orm pgyarn add -D drizzle-kit @types/pg
pnpm add drizzle-orm pgpnpm add -D drizzle-kit @types/pg
bun add drizzle-orm pgbun add -D drizzle-kit @types/pg

Step 2 - Initialize the driver and make a query

node-postgres
node-postgres with config
// Make sure to install the 'pg' packageimport { drizzle }from 'drizzle-orm/node-postgres';const db = drizzle(process.env.DATABASE_URL);const result = await db.execute('select 1');
// Make sure to install the 'pg' packageimport { drizzle }from 'drizzle-orm/node-postgres';// You can specify any property from the node-postgres connection optionsconst db = drizzle({  connection: {    connectionString: process.env.DATABASE_URL,    ssl: true  }});const result = await db.execute('select 1');

If you need to provide your existing driver:

// Make sure to install the 'pg' packageimport { drizzle }from "drizzle-orm/node-postgres";import { Pool }from "pg";const pool = new Pool({  connectionString: process.env.DATABASE_URL,});const db = drizzle({ client: pool });const result = await db.execute('select 1');

postgres.js

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm postgresnpm i -D drizzle-kit
yarn add drizzle-orm postgresyarn add -D drizzle-kit
pnpm add drizzle-orm postgrespnpm add -D drizzle-kit
bun add drizzle-orm postgresbun add -D drizzle-kit

Step 2 - Initialize the driver and make a query

postgres.js
postgres.js with config
import { drizzle }from 'drizzle-orm/postgres-js';const db = drizzle(process.env.DATABASE_URL);const result = await db.execute('select 1');
import { drizzle }from 'drizzle-orm/postgres-js';// You can specify any property from the postgres-js connection optionsconst db = drizzle({  connection: {    url: process.env.DATABASE_URL,    ssl: true  }});const result = await db.execute('select 1');

If you need to provide your existing driver:

// Make sure to install the 'postgres' packageimport { drizzle }from 'drizzle-orm/postgres-js';import postgresfrom 'postgres';const queryClient = postgres(process.env.DATABASE_URL);const db = drizzle({ client: queryClient });const result = await db.execute('select 1');

What’s next?

Manage schema

Drizzle Schema PostgreSQL data types Indexes and Constraints Database Views Database Schemas Sequences Extensions

Query data

Relational Queries Select Insert Update Delete Filters Joins sql`` operator

[8]ページ先頭

©2009-2025 Movatter.jp