Promote a branch
Learn how to promote a branch to the default branch of your Neon project using the Neon API
This guide describes how to create a new branch and promote it to the default branch of your Neon project in the context of a data recovery scenario. It also describes how to move the compute endpoint from your existing default branch to the new branch to avoid having to reconfigure your application's database connection details.
What is a default branch?
Each Neon project has a default branch. In the Neon Console, your default branch is identified on the Branches page by a DEFAULT
tag. You can designate any branch as the default branch. The advantage of the default branch is that its compute endpoint remains accessible if you exceed your project's limits, ensuring uninterrupted access to data that resides on the default branch, which is typically the branch used in production.
- For Neon Free Tier users, the compute endpoint associated with the default branch is always available.
- For users on paid plans, the compute endpoint associated with the default branch is exempt from the limit on simultaneously active computes, ensuring that it is always available. Neon has a default limit of 20 simultaneously active computes to protect your account from unintended usage.
Why promote a branch to default?
A common usage scenario involving promoting a branch to default is data recovery. For example, a data loss occurs on the current default branch. To recover the lost data, you create a point-in-time branch with data that existed before the data loss occurred. To avoid modifying your application's database connection configuration, you move the compute endpoint from the current default branch to the new branch and make that branch your default branch.
The procedure described below creates a new branch and promotes it to the default branch of your project by performing the following steps:
- Creating a new point-in-time branch without a compute endpoint
- Moving the compute endpoint from your current default branch to the new branch
- Renaming the old default branch
- Renaming the new branch to the name of the old default branch
- Promoting the new branch to default
Prerequisites
The following information is required to perform the procedure:
- A Neon API key. For information about obtaining an API key, see Create an API key.
- The
project_id
for your Neon project. You can obtain aproject_id
using the List projects method, or you can find it on your project's Project settings page in the Neon Console. - The
branch_id
of the current default branch. You can obtain abranch_id
using the List branches method, or you can find it on the your project's Branches page in the Neon Console. Abranch_id
has abr-
prefix. - The
endpoint_id
of the compute endpoint associated with the current default branch. You can obtain anendpoint_id
using the List endpoints method, or you can find it on the Branches page in the Neon Console. Anendpoint_id
has anep-
prefix.
Creating a new point-in-time branch without a compute endpoint
The Create branch request shown below creates a point-in-time branch without a compute endpoint. The project_id
is a required parameter. To create a point-in-time branch, specify a parent_timestamp
value in the branch
object. The parent_timestamp
value must be provided in ISO 8601 format. You can use this timestamp converter. For more information about point-in-time restore, see Branching — Point-in-time restore (PITR).
The project_id
value used in the example below is young-silence-08999984
. You must also set the $NEON_API_KEY
variable or replace $NEON_API_KEY
with an actual API key. The branch is given the name recovery_branch
. You will change the name in a later step.
The response body includes the id
of your new branch. You will need this value (br-solitary-hat-85369851
) to move the compute endpoint in the next step.
Response body
note
Creating a point-in-time branch can also be performed using the Neon Console or CLI. See Create a point-in-time branch for Neon Console instructions. See Neon CLI commands — branches for CLI instructions.
Move the compute endpoint from your current default branch to the new branch
The Update endpoint API request shown below moves the compute endpoint from your current default branch to the new branch. The required parameters are the project_id
and endpoint_id
of your current default branch, and the branch_id
of the new branch. You must also set the $NEON_API_KEY
variable or replace $NEON_API_KEY
with an actual API key.
Response body
note
This procedure can only be performed using the Neon API. You can expect Neon Cole and CLI support to be added in a future release.
Rename the old default branch
The Update branch API request shown below renames the old default branch to old_main
. You may want to delete this branch later to reduce storage usage, but just rename it for now. The required parameters are the project_id
and branch_id
. You must also set the $NEON_API_KEY
variable or replace $NEON_API_KEY
with an actual API key.
Response body
note
Renaming a branch can also be performed using the Neon Console or CLI. See Rename a branch for Neon Console instructions. See Neon CLI commands — branches for CLI instructions.
Rename the new branch to the name of the old default branch
Rename the new branch to the name of the old branch, which was main
. The Update branch API request shown below renames the new branch from recovery_branch
to main
.
Response body
note
Renaming a branch can also be performed using the Neon Console or CLI. See Rename a branch for Neon Console instructions. See Neon CLI commands — branches for CLI instructions.
Promote the new branch to default
The Set default branch API request sets the new branch as the default branch for the project.
Response body
note
Promoting a branch to default can also be performed using the Neon Console or CLI. See Set a branch as primary for Neon Console instructions. See Neon CLI commands — branches for CLI instructions.
You should now have a new default branch, and because you moved the compute endpoint from your old default branch to the new one, you do not need to change the connection details in your applications. Once you have validated the change, consider deleting your old default branch to save storage space. See Delete a branch with the API.
Last updated on