--- id: 'serverless-drivers' title: 'Serverless drivers' description: 'Connect to your Postgres database from a serverless environment' subtitle: 'Choose a driver for connecting to your Postgres database from a serverless environment.' --- Learn how to connect to your Postgres database from a serverless environment. The driver you use depends on which runtime your code runs in. [supabase-js](/docs/reference/javascript/introduction) is an isomorphic JavaScript client that uses the [auto-generated REST API](/docs/guides/api), so it works in any environment that supports HTTPS connections. This API has a built-in [connection pooler](/docs/guides/database/connecting-to-postgres/pooling-and-limits#how-connection-pooling-works) and can serve thousands of simultaneous requests, which suits serverless workloads. If you connect with a Postgres client instead, use the [shared pooler in transaction mode](/docs/guides/database/connecting-to-postgres#pooler-transaction-mode), then [configure your client](/docs/guides/database/connecting-to-postgres#configure-your-client) for pool size, prepared statements, and SSL. Those three settings are what most serverless connection problems come down to. ## Vercel Edge Functions Vercel's [Edge runtime](https://vercel.com/docs/functions/runtimes/edge-runtime) runs on the [V8 engine](https://v8.dev/) and exposes a limited set of Web Standard APIs. Start from a deploy template, or configure the connection yourself. ### Quickstart Choose one of these Vercel Deploy Templates. They use the [Vercel Deploy Integration](https://vercel.com/integrations/supabase) to configure your connection strings as environment variables on your Vercel project.
A Next.js App Router template configured with cookie-based auth using Supabase, TypeScript and Tailwind CSS. Basic Next.js template that uses Supabase as the database and Kysely as the query builder.
### Manual configuration 1. In the Supabase Dashboard, click [Connect](/dashboard/project/_?showConnect=true&method=transaction) and copy the URI from the **Transaction pooler** section. 2. Replace the password placeholder with your database password. 3. Add the suffix `?workaround=supabase-pooler.vercel` to the URI. 4. Save the result as the `POSTGRES_URL` environment variable. ```txt .env.local POSTGRES_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:6543/postgres?workaround=supabase-pooler.vercel" ``` ```ts lib/drizzle.ts import { sql } from '@vercel/postgres' import { InferInsertModel, InferSelectModel } from 'drizzle-orm' import { pgTable, serial, text, timestamp, uniqueIndex } from 'drizzle-orm/pg-core' import { drizzle } from 'drizzle-orm/vercel-postgres' export const UsersTable = pgTable( 'users', { id: serial('id').primaryKey(), name: text('name').notNull(), email: text('email').notNull(), image: text('image').notNull(), createdAt: timestamp('createdAt').defaultNow().notNull(), }, (users) => { return { uniqueIdx: uniqueIndex('unique_idx').on(users.email), } } ) export type User = InferSelectModel export type NewUser = InferInsertModel // Connect to Vercel Postgres export const db = drizzle(sql) ``` ```ts lib/kysely.ts import { createKysely } from '@vercel/postgres-kysely' import { ColumnType, Generated } from 'kysely' interface UserTable { // Columns that are generated by the database should be marked // using the `Generated` type. This way they are automatically // made optional in inserts and updates. id: Generated name: string email: string image: string // You can specify a different type for each operation (select, insert and // update) using the `ColumnType` // wrapper. Here we define a column `createdAt` that is selected as // a `Date`, can optionally be provided as a `string` in inserts and // can never be updated: createdAt: ColumnType } // Keys of this interface are table names. export interface Database { users: UserTable } export const db = createKysely() export { sql } from 'kysely' ``` ## Cloudflare Workers Cloudflare's Workers runtime also uses the [V8 engine](https://v8.dev/), but it provides polyfills for a subset of Node.js APIs and the [TCP Sockets API](https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/). That gives you three options: ### Drivers [#cloudflare-workers-drivers] - [supabase-js](https://developers.cloudflare.com/workers/databases/native-integrations/supabase/) - [Postgres.js](https://github.com/porsager/postgres?tab=readme-ov-file#cloudflare-workers-support) - [node-postgres](https://developers.cloudflare.com/workers/tutorials/postgres/) ## Supabase Edge Functions Supabase Edge Functions use the [Deno runtime](https://deno.com/), which has native support for TCP connections. You can choose any of these clients: ### Drivers [#supabase-edge-functions-drivers] - [supabase-js](/docs/guides/functions/connect-to-postgres#using-supabase-js) - [Deno Postgres driver](/docs/guides/functions/connect-to-postgres#using-a-postgres-client) - [Postgres.js](https://github.com/porsager/postgres) - [Drizzle](/docs/guides/functions/connect-to-postgres#using-drizzle)