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
xata init once in your project folder to link it:
xata branch url fail with
Missing required path parameter: organizationID.
Connect a Netlify site to Xata
- In the Xata console, open the branch you want the site to use.
- On the Overview tab, copy the pooled connection string. You can also get it from the CLI:
- In Netlify, go to Project configuration > Environment variables and add a variable named
DATABASE_URLwith 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. - 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.- On the branch’s Queries page in Xata, create the table used by this example:
- Query the database from a Netlify Function:
- Deploy the site. The function now reads from your Xata branch.
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-scopedDATABASE_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
DATABASE_URL you set on the site.
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.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. Usexata 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
- Branching: how copy-on-write branches work
- Scale to Zero: how idle branches hibernate and wake
- Serverless Proxy: HTTP and WebSocket connections for functions and edge runtimes