Schema diff
Learn how to use Neon's Schema Diff tool to compare branches of your database
Neon's Schema Diff tool lets you compare an SQL script of the schemas for two selected branches in a side-by-side view (or line-by-line on mobile devices).
How Schema Diff works
Schema Diff is available in the Neon Console for use in two ways:
- Compare a branch's schema to its parent
- Compare selected branches during a branch restore operation
You can also use the branches schema-diff
command in the Neon CLI or compare-schema
endpoint in the Neon API to effect a variety of comparisons.
Compare to parent
In the detailed view for any child branch, you can check the schema differences between the selected branch and its parent. Use this view to verify the state of these schemas before you Reset from parent.
Compare to another branch's history
Built into the Time Travel assist editor, you can use Schema Diff to help when restoring branches, letting you compare states of your branch against its own or another branch's history before you complete a branch restore operation.
Comparisons using the CLI or API
You can use the Neon CLI to compare a branch to any point in its own or any other branch's history. The branches schema-diff
command offers full flexibility for any type of schema comparison: between a branch and its parent, a branch and its earlier state, or a branch to the head or prior state of another branch. The Neon API provides a compare-schema
endpoint that lets you compare schemas between Neon branches programmatically, supporting CI/CD automation and AI agent use cases.
Practical Applications
- Pre-Migration Reviews: Before migrating schemas from a development branch into main, use Schema Diff to ensure only intended schema changes are applied.
- Audit Changes: Historically compare schema changes to understand the evolution of your database structure.
- Consistency Checks: Ensure environment consistency by comparing schemas across development, staging, and production branches.
- Automation: Integrate schema-diff into CI/CD pipelines to automatically compare schemas during deployments.
- AI Agents: Enable AI agents to retrieve schema differences programmatically to support agent-driven database migrations.
How to Use Schema Diff
You can launch the Schema Diff viewer from the Branches and Restore pages in the Neon Console.
From the Branches page
Open the detailed view for the branch whose schema you want to inspect. In the row of details for the parent branch, under the COMPARE TO PARENT block, click Open schema diff.
From the Restore page
Just like with Time Travel Assist, your first step is to choose the branch you want to restore, then choose where you want to restore from: From history (its own history) or ** From another branch** (from another branch's history).
Click the Schema Diff button, verify that your selections are correct, then click Compare.
The two-pane view shows the schema for both your target and your selected branches.
Using the Neon CLI
You can use the Neon CLI to:
- Compare the latest schemas of any two branches
- Compare against a specific point in its own or another branch’s history
Use the schema-diff
subcommand from the branches
command:
The operation will compare a selected branch ([compare-source]
) against the latest (head) of your base branch ([base-branch]
). For example, if you want to compare recent changes you made to your development branch dev/alex
against your production branch main
, identify main
as your base branch and dev/alex
as your compare-source.
You have a few options here:
- Append a timestamp or LSN to compare to a specific point in
dev/alex
branch's history. - If you are regularly comparing development branches against
main
, includemain
in yourset-context
file. You can then leave out the [base-branch] from the command. - Use aliases to shorten the command.
- Include
--database
to reduce the diff to a single database. If you don't specify a database, the diff will include all databases on the branch.
Here is the same command using aliases, with main
included in set-context
, pointing to an LSN from dev/alex
branch's history, and limiting the diff to the database people
:
To find out what other comparisons you can make, see Neon CLI commands — branches for full documentation of the command.
Using the Neon API
The compare_schema
endpoint lets you compare schemas between Neon branches programmatically and track schema changes. The endpoint response highlights differences in a diff
format, making it useful for automating schema migrations and integrating schema checks into CI/CD workflows.
Another use case for schema diff via the Neon API is AI agent-driven workflows, such as environment setup and schema migrations. The compare_schema
endpoint allows AI agents to programmatically retrieve schema differences by comparing two branches.
To compare schemas between two branches, use a cURL command similar to the following:
Parameter | Description | Required | Example |
---|---|---|---|
<project_id> | The ID of your Neon project. | Yes | wispy-butterfly-25042691 |
<branch_id> | The ID of the target branch to compare. | Yes | br-rough-boat-a54bs9yb |
<base_branch_id> | The ID of the base branch for comparison. | Yes | br-royal-star-a54kykl2 |
<db_name> | The name of the database in the target branch. | Yes | neondb |
Authorization | Bearer token for API access (your Neon API key) | Yes | $NEON_API_KEY |
note
The optional jq -r '.diff'
command extracts the diff field from the JSON response and outputs it as plain text to make it easier to read. This command would not be necessary when using the endpoint programmatically.
Here’s an example of the compare_schema
diff output for the neondb
database after comparing br-rough-boat-a54bs9yb
with the base branch br-royal-star-a54kykl2
, where a new column was added to the target branch.
Output explanation:
-
(minus): Lines that were removed from the base branch schema.+
(plus): Lines that were added in the target branch schema.
In the example above, the created_at
column was added to the public.playing_with_neon
table.
Schema Diff GitHub Action
Neon supports a Schema Diff GitHub Action that performs a database schema diff on specified Neon branches for each pull request and writes a comment to the pull request highlighting the schema differences.
This action supports workflows where schema changes are made on a branch. When you create or update a pull request containing schema changes, the action automatically generates a comment within the pull request. By including the schema diff as part of the comment, reviewers can easily assess the changes directly within the pull request.
To learn more, see the Schema Diff GitHub Action.
Tutorial
For a step-by-step guide showing you how to compare two development branches using Schema Diff, see Schema diff tutorial.