Drizzle <> PostgreSQL
This guide assumes familiarity with:
- Databaseconnection basics with Drizzle
- node-postgresbasics
- postgres.jsbasics
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:
- With
node-postgres, you can installpg-nativeto boost the speed of bothnode-postgresand Drizzle by approximately 10%. node-postgressupports providing type parsers on a per-query basis without globally patching things. For more details, seeTypes Docs.postgres.jsuses prepared statements by default, which you may need to opt out of. This could be a potential issue in AWS environments, among others, so please keep that in mind.- If there’s anything else you’d like to contribute, we’d be happy to receive your PRshere
node-postgres
Step 1 - Install packages
npm
yarn
pnpm
bun
npm i drizzle-orm pgnpm i -D drizzle-kit @types/pgyarn add drizzle-orm pgyarn add -D drizzle-kit @types/pgpnpm add drizzle-orm pgpnpm add -D drizzle-kit @types/pgbun add drizzle-orm pgbun add -D drizzle-kit @types/pgStep 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-kityarn add drizzle-orm postgresyarn add -D drizzle-kitpnpm add drizzle-orm postgrespnpm add -D drizzle-kitbun add drizzle-orm postgresbun add -D drizzle-kitStep 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');