Become a Gold Sponsor

Drizzle <> Xata

This guide assumes familiarity with:

According their official website, Xata is a Postgres data platform with a focus on reliability, scalability, and developer experience. The Xata Postgres service is currently in beta, please see the Xata docs on how to enable it in your account.

Drizzle ORM natively supports both the xata driver with drizzle-orm/xata package and the postgres or pg drivers for accessing a Xata Postgres database.

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm @xata.io/client
npm i -D drizzle-kit

You can use Drizzle with Xata with a HTTP client or a TCP client. The HTTP client doesnโ€™t create a persistent connection to the Xata server, while the TCP client does and can be used for better performance with a higher number of requests. The HTTP client is usually recommended from serverless environments like Cloudflare Workers or Vercel Edge Functions. The TCP client is typically used from long-running servers like Express.js or Fastify.

The following example use the Xata generated client, which you obtain by running the xata init CLI command.

HTTP
TCP
TCP (pool)
import { drizzle } from 'drizzle-orm/xata-http';
import { getXataClient } from './xata'; // Generated client

const xata = getXataClient();
const db = drizzle(xata);

const result = await db.select().from(...);

If you prefer to not use the generated Xata client, it is also possible to use Xata with the postgres or pg drivers, in this case you can copy the connection string from the Settings page of your Xata database. For more information, please check our PostgreSQL connections section

For more details about using Drizzle with Xata, see the official Xata docs.

Whatโ€™s next?