Neon CLI commands — branches
Use the Neon CLI to manage Neon directly from the terminal
Before you begin
- Before running the
branches
command, ensure that you have installed the Neon CLI. - If you have not authenticated with the neon auth command, running a Neon CLI command automatically launches the Neon CLI browser authentication process. Alternatively, you can specify a Neon API key using the
--api-key
option when running a command. See Connect.
branches
command
The The branches
command allows you to list, create, rename, delete, and retrieve information about branches in your Neon project. It also permits setting a branch as the default branch, adding a compute to a branch, adding a read replica, or perforning a schema diff between different branches.
Usage
Subcommand | Description |
---|---|
list | List branches |
create | Create a branch |
reset | Reset data to parent |
restore | Restore a branch to a selected point in time |
rename | Rename a branch |
schema-diff | Compare schemas |
set-default | Set a default branch |
add-compute | Add replica to a branch |
delete | Delete a branch |
get | Get a branch |
list
This subcommand allows you to list branches in a Neon project.
Usage
Options
In addition to the Neon CLI global options, the list
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
Examples
-
List branches with the default
table
output format. The information provided with this output format is limited compared to other formats, such asjson
. -
List branches with the
json
output format. This format provides more information than the defaulttable
output format.
create
This subcommand allows you to create a branch in a Neon project.
Usage
Options
In addition to the Neon CLI global options, the create
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
--name | The branch name | string | |
--parent | Parent branch name, id, timestamp, or LSN. Defaults to the default branch | string | |
--compute | Create a branch with or without a compute. By default, the branch is created with a read-write endpoint. The default value is true . To create a branch without a compute, use --no-compute | boolean | |
--type | Type of compute to add. Choices are read_write (the default) or read_only . A read-only compute is a read replica. | string | |
--suspend-timeout | Duration of inactivity in seconds after which the compute is automatically suspended. The value 0 means use the global default. The value -1 means never suspend. The default value is 300 seconds (5 minutes). The maximum value is 604800 seconds (1 week). | number | |
--cu | The number of Compute Units. Could be a fixed size (e.g. "2") or a range delimited by a dash (e.g. "0.5-3"). | string | |
--psql | Connect to a new branch via psql . psql must be installed to use this option. | boolean |
note
When creating a branch from a protected parent branch, role passwords on the child branch are changed. For more information about this Protected Branches feature, see New passwords generated for Postgres roles on child branches.
Examples
-
Create a branch:
note
If the parent branch has more than one role or database, the
branches create
command does not output a connection URI. As an alternative, you can use theconnection-string
command to retrieve the connection URI for a branch. This command includes options for specifying the role and database. See Neon CLI commands — connection-string. -
Create a branch with the
--output
format of the command set tojson
. This output format returns all of the branch response data, whereas the defaulttable
output format (shown in the preceding example) is limited in the information it can display.Example output
-
Create a branch with a user-defined name:
-
Set the compute size when creating a branch:
-
Set the compute's autoscaling range when creating a branch:
-
Create a branch with a read replica compute.
-
Create a branch from a parent branch other than your
main
branch -
Create a point-in-time restore branch by specifying the
--parent
option with a timestamp:The timestamp 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).
-
Create a branch and connect to it with
psql
. -
Create a branch, connect to it with
psql
, and run an.sql
file. -
Create a branch, connect to it with
psql
, and run a query.
reset
This command resets a child branch to the latest data from its parent.
Usage
<id|name>
refers to the branch ID or branch name. You can use either one for this operation.
--parent
specifies the type of reset operation. Currently, Neon only supports reset from parent. This parameter is required for the operation to work. In the future, Neon might add support for other reset types: for example, rewinding a branch to an earlier period in time.
Options
In addition to the Neon CLI global options, the reset
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project or context is not set |
--parent | Reset to a parent branch | boolean | |
--preserve-under-name | The name under which to preserve the old branch | string |
Example
restore
This command restores a branch to a specified point in time in its own or another branch's history.
Usage
<target-id|name>
specifies the ID or name of the branch that you want to restore.
<source>
specifies the source branch you want to restore from. Options are:
^self
— restores the selected branch to an earlier point in its own history. You must select a timestamp or LSN for this option (restoring to head is not an option). You also need to include a name for the backup branch using the parameterpreserve-under-name
.^parent
— restores the target branch to its parent. By default the target is restored the latest (head) of its parent. Append@timestamp
or@lsn
to restore to an earlier point in the parent's history.source branch ID
orsource branch name
— restores the target branch to the selected source branch. It restores the latest (head) by default. Append@timestamp
or@lsn
to restore to an earlier point in the source branch's history.
Options
In addition to the Neon CLI global options, the restore
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project or context is not set |
--preserve-under-name | Name for the backup created during restore. | string | When restoring to ^self |
Examples
Examples of the different kinds of restore operations you can do:
- Restoring a branch to an earlier point in its history
- Restoring to another branch's head
- Restoring a branch to its parent
Restoring a branch to an earlier point in its own history (with backup)
This command restores the branch main
to an earlier timestamp, saving to a backup branch called main_restore_backup_2024-02-20
Results of the operation:
Restoring a branch (target) to the head of another branch (source)
This command restores the target branch dev/alex
to latest data (head) from the source branch main
.
Results of the operation:
Restoring a branch to its parent at an earlier point in time
This command restores the branch dev/alex
to a selected point in time from its parent branch.
Results of the operation:
rename
This subcommand allows you to update a branch in a Neon project.
Usage
<id|name>
refers to the Branch ID and branch name. You can specify one or the other.
Options
In addition to the Neon CLI global options, the rename
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
Example
schema-diff
This command:
- Compares the latest schemas of any two branches
- Compares against a specific point in its own or another branch’s history
Usage
[base-branch]
specifies the branch you want to compare against. For example, if you want to compare a development branch against the production branch main
, select main
as your base.
This setting is optional. If you leave it out, the operation uses either of the following as the base:
- The branch identified in the
set-context
file - If no context is configured, it uses your project's default branch
[compare-source]
specifies the branch or state to compare against. Options are:
^self
— compares the selected branch to an earlier point in its own history. You must specify a timestamp or LSN.^parent
— compares the selected branch to the head of its parent branch. You can append@timestamp
or@lsn
to compare to an earlier point in the parent's history.<compare-branch-id|name>
— compares the selected branch to the head of another specified branch. Append@timestamp
or@lsn
to compare to an earlier point in the specified branch's history.
Options
In addition to the Neon CLI global options, the schema-diff
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project or context is not set |
--database , --db | Name of the database for which the schema comparison is performed | string |
note
The --no-color
or --color false
global option can be used to decolorize the CLI command output when using CLI commands in CI/CD pipelines.
Examples
Examples of different kinds of schema diff operations you can do:
- Compare to another branch's head
- Compare to an earlier point in a branch's history
- Compare a branch to its parent
- Compare to an earlier point in another branch's history
Compare to another branch's head
This command compares the schema of the main
branch to the head of the branch dev/alex
.
The output indicates that in the table public.playing_with_neon
, a new column description character varying(255)
has been added in the dev/alex
branch that is not present in the main
branch.
Comparing a branch to an earlier point in its history
This command compares the schema of dev-alex
to a previous state in its history at LSN 0/123456.
Comparing a branch to its parent
This command compares the schema of dev/alex
to the head of its parent branch.
Comparing a branch to an earlier point in another branch's history
This command compares the schema of the main
branch to the state of the dev/jordan
branch at timestamp 2024-06-01T00:00:00.000Z
.
set-default
This subcommand allows you to set a branch as the default branch in your Neon project.
Usage
<id|name>
refers to the Branch ID and branch name. You can specify one or the other.
Options
In addition to the Neon CLI global options, the set-default
subcommand supports this option:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
Example
add-compute
This subcommand allows you to add a compute to an existing branch in your Neon project.
Usage
<id|name>
refers to the Branch ID and branch name. You can specify one or the other.
Options
In addition to the Neon CLI global options, the add-compute
subcommand supports these options:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
--type | Type of compute to add. Choices are read_only (the default) or read_write . A read-only compute is a read replica. A branch can have a single primary read-write compute and multiple read replica computes. | string | |
--cu | Sets the compute size in Compute Units. For a fixed size, enter a single number (e.g., "2"). For autoscaling, enter a range with a dash (e.g., "0.5-3"). | string |
Examples
-
Add a read replica compute (a read replica) to a branch:
-
Set the compute size when adding a compute to a branch:
-
Set the compute's autoscaling range when adding a compute to a branch:
delete
This subcommand allows you to delete a branch in a Neon project.
Usage
<id|name>
refers to the Branch ID and branch name. You can specify one or the other.
Options
In addition to the Neon CLI global options, the delete
subcommand supports this option:
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
Example
get
This subcommand allows you to retrieve details about a branch.
Usage
Options
In addition to the Neon CLI global options, the get
subcommand supports this option:
Options
Option | Description | Type | Required |
---|---|---|---|
--context-file | Context file path and file name | string | |
--project-id | Project ID | string | Only if your Neon account has more than one project |
Examples
A get
example with the --output
format option set to json
:
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.