Skip to main content

What is Netlify

Netlify is a platform for building and deploying web applications, with Git-driven builds, serverless functions, and a Deploy Preview for every pull request. Xata gives a Netlify site a managed PostgreSQL database, and gives each Deploy Preview its own branch of that database with production-like data. Branches are copy-on-write, so a preview branch takes about 3 seconds to create and stores only its diff from the parent. Idle branches scale to zero, so previews that nobody is looking at cost nothing to run.

Prerequisites

  • A Xata account and a project with at least one branch
  • A Netlify site
  • The Xata CLI installed, if you want to use the CLI commands below or per-preview branches
The CLI commands in this guide read the organization, project, and branch from the folder they run in. Run xata init once in your project folder to link it:
Without this, commands such as xata branch url fail with Missing required path parameter: organizationID.

Connect a Netlify site to Xata

  1. In the Xata console, open the branch you want the site to use.
  2. On the Overview tab, copy the pooled connection string. You can also get it from the CLI:
Use the pooler connection type for Netlify Functions. Serverless invocations create many short-lived connections, and the pooler keeps the number of PostgreSQL connections low under concurrency. See Serverless Proxy for the full list of connection types.
  1. In Netlify, go to Project configuration > Environment variables and add a variable named DATABASE_URL with the connection string as its value. Leave the scopes at their default so the variable is available to builds and functions on every Netlify plan.
  2. Install the driver in your project:
Xata implements the Neon serverless driver protocol, so the @neondatabase/serverless package connects to Xata over HTTP and WebSocket without changes. If you are moving a Netlify site off Neon, your data access code stays as it is.
  1. On the branch’s Queries page in Xata, create the table used by this example:
  1. Query the database from a Netlify Function:
  1. Deploy the site. The function now reads from your Xata branch.
Standard TCP connections work too, so ORMs and drivers such as Drizzle, Prisma, and pg connect with the same connection string. For long-running processes rather than functions, use xata branch url main without the --type pooler flag.

Give every Deploy Preview its own database

Netlify builds a Deploy Preview for each pull request. To pair each preview with its own database, create a Xata branch when the pull request opens and set a branch-scoped DATABASE_URL on the Netlify site. This uses a GitHub Actions workflow rather than a Netlify build plugin, because the connection string has to exist before the build starts and has to be readable by the deployed functions at runtime.

Create the branch when a pull request opens

Netlify applies a branch-scoped value to that branch’s Deploy Previews, branch deploys, and deploy permalinks, so the preview build and its functions both pick up the branch connection string. Production keeps whatever DATABASE_URL you set on the site.
The CLI reads XATA_API_KEY from the environment, but not the organization or project. Outside a folder linked with xata init, pass --organization and --project on every command, as the workflow above does. Without them each command fails with Missing required path parameter: projectID.

Clean up when the pull request closes

XATA_API_KEY should be an organization API key. Create one with xata keys organization create. The other IDs come from xata organization get id, xata project get id, and xata branch get id. Set XATA_PREVIEW_PARENT_BRANCH_ID to the ID of the branch that previews should copy. Run these commands from a folder you have linked with xata init, on CLI 1.7.0 or later.
For the full set of Xata GitHub Actions, including schema migration checks on pull requests, see Automations.

Anonymize preview data

Deploy Previews on a public repository can be triggered by anyone who opens a pull request, so a preview branch forked from production may expose real customer data. Branch creation copies its parent’s data without anonymizing it. Use xata clone to create an anonymized staging branch, then set XATA_PREVIEW_PARENT_BRANCH_ID to that branch’s ID. Each preview branch will inherit the sanitized data. See Netlify’s sensitive variable policy for controlling which deploys can read your variables.

Next steps