neon 2.1.1 → 2.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/LICENSE.md +178 -0
  2. package/README.md +601 -0
  3. package/dist/analytics.js +156 -0
  4. package/dist/api.js +665 -0
  5. package/dist/auth.js +127 -0
  6. package/dist/callback.html +51 -0
  7. package/dist/cli.js +9 -0
  8. package/dist/commands/auth.js +214 -0
  9. package/dist/commands/bootstrap.js +481 -0
  10. package/dist/commands/branches.js +481 -0
  11. package/dist/commands/bucket.js +543 -0
  12. package/dist/commands/checkout.js +289 -0
  13. package/dist/commands/config.js +544 -0
  14. package/dist/commands/connection_string.js +172 -0
  15. package/dist/commands/data_api.js +285 -0
  16. package/dist/commands/databases.js +82 -0
  17. package/dist/commands/deploy.js +26 -0
  18. package/dist/commands/dev.js +698 -0
  19. package/dist/commands/env.js +166 -0
  20. package/dist/commands/functions.js +373 -0
  21. package/dist/commands/index.js +54 -0
  22. package/dist/commands/init.js +73 -0
  23. package/dist/commands/ip_allow.js +137 -0
  24. package/dist/commands/link.js +1121 -0
  25. package/dist/commands/neon_auth.js +1028 -0
  26. package/dist/commands/operations.js +28 -0
  27. package/dist/commands/orgs.js +24 -0
  28. package/dist/commands/projects.js +372 -0
  29. package/dist/commands/psql.js +62 -0
  30. package/dist/commands/roles.js +65 -0
  31. package/dist/commands/schema_diff.js +151 -0
  32. package/dist/commands/set_context.js +29 -0
  33. package/dist/commands/status.js +40 -0
  34. package/dist/commands/user.js +15 -0
  35. package/dist/commands/vpc_endpoints.js +134 -0
  36. package/dist/config.js +11 -0
  37. package/dist/config_format.js +72 -0
  38. package/dist/context.js +177 -0
  39. package/dist/current_branch_fast_path.js +55 -0
  40. package/dist/dev/env.js +240 -0
  41. package/dist/dev/functions.js +70 -0
  42. package/dist/dev/inputs.js +63 -0
  43. package/dist/dev/runtime.js +146 -0
  44. package/dist/env.js +36 -0
  45. package/dist/env_file.js +159 -0
  46. package/dist/errors.js +80 -0
  47. package/dist/functions_api.js +44 -0
  48. package/dist/help.js +146 -0
  49. package/dist/index.js +234 -0
  50. package/dist/log.js +18 -0
  51. package/dist/parameters.gen.js +480 -0
  52. package/dist/pkg.js +25 -0
  53. package/dist/psql/cli.js +53 -0
  54. package/dist/psql/command/cmd_cond.js +437 -0
  55. package/dist/psql/command/cmd_connect.js +820 -0
  56. package/dist/psql/command/cmd_copy.js +1035 -0
  57. package/dist/psql/command/cmd_describe.js +1815 -0
  58. package/dist/psql/command/cmd_format.js +922 -0
  59. package/dist/psql/command/cmd_io.js +2193 -0
  60. package/dist/psql/command/cmd_lo.js +393 -0
  61. package/dist/psql/command/cmd_meta.js +970 -0
  62. package/dist/psql/command/cmd_misc.js +187 -0
  63. package/dist/psql/command/cmd_pipeline.js +1148 -0
  64. package/dist/psql/command/cmd_restrict.js +171 -0
  65. package/dist/psql/command/cmd_show.js +766 -0
  66. package/dist/psql/command/dispatch.js +343 -0
  67. package/dist/psql/command/inputQueue.js +42 -0
  68. package/dist/psql/command/shared.js +71 -0
  69. package/dist/psql/complete/filenames.js +139 -0
  70. package/dist/psql/complete/index.js +104 -0
  71. package/dist/psql/complete/matcher.js +315 -0
  72. package/dist/psql/complete/psqlVars.js +247 -0
  73. package/dist/psql/complete/queries.js +493 -0
  74. package/dist/psql/complete/rules.js +2424 -0
  75. package/dist/psql/core/common.js +1253 -0
  76. package/dist/psql/core/help.js +576 -0
  77. package/dist/psql/core/mainloop.js +1360 -0
  78. package/dist/psql/core/prompt.js +440 -0
  79. package/dist/psql/core/settings.js +684 -0
  80. package/dist/psql/core/sqlHelp.js +1066 -0
  81. package/dist/psql/core/startup.js +846 -0
  82. package/dist/psql/core/syncVars.js +116 -0
  83. package/dist/psql/core/variables.js +287 -0
  84. package/dist/psql/describe/formatters.js +1290 -0
  85. package/dist/psql/describe/processNamePattern.js +270 -0
  86. package/dist/psql/describe/queries.js +2378 -0
  87. package/dist/psql/describe/versionGate.js +43 -0
  88. package/dist/psql/index.js +2030 -0
  89. package/dist/psql/io/history.js +299 -0
  90. package/dist/psql/io/input.js +120 -0
  91. package/dist/psql/io/lineEditor/buffer.js +325 -0
  92. package/dist/psql/io/lineEditor/complete.js +227 -0
  93. package/dist/psql/io/lineEditor/filename.js +159 -0
  94. package/dist/psql/io/lineEditor/index.js +894 -0
  95. package/dist/psql/io/lineEditor/keymap.js +745 -0
  96. package/dist/psql/io/lineEditor/vt100.js +363 -0
  97. package/dist/psql/io/pgpass.js +202 -0
  98. package/dist/psql/io/pgservice.js +194 -0
  99. package/dist/psql/io/psqlrc.js +422 -0
  100. package/dist/psql/print/aligned.js +1765 -0
  101. package/dist/psql/print/asciidoc.js +248 -0
  102. package/dist/psql/print/crosstab.js +463 -0
  103. package/dist/psql/print/csv.js +95 -0
  104. package/dist/psql/print/html.js +258 -0
  105. package/dist/psql/print/json.js +96 -0
  106. package/dist/psql/print/latex.js +396 -0
  107. package/dist/psql/print/pager.js +267 -0
  108. package/dist/psql/print/troff.js +258 -0
  109. package/dist/psql/print/unaligned.js +119 -0
  110. package/dist/psql/print/units.js +135 -0
  111. package/dist/psql/scanner/slash.js +515 -0
  112. package/dist/psql/scanner/sql.js +914 -0
  113. package/dist/psql/scanner/stringutils.js +394 -0
  114. package/dist/psql/types/backslash.js +1 -0
  115. package/dist/psql/types/connection.js +1 -0
  116. package/dist/psql/types/index.js +7 -0
  117. package/dist/psql/types/printer.js +1 -0
  118. package/dist/psql/types/repl.js +1 -0
  119. package/dist/psql/types/scanner.js +24 -0
  120. package/dist/psql/types/settings.js +1 -0
  121. package/dist/psql/types/variables.js +1 -0
  122. package/dist/psql/wire/connection.js +2858 -0
  123. package/dist/psql/wire/copy.js +108 -0
  124. package/dist/psql/wire/notify.js +59 -0
  125. package/dist/psql/wire/pipeline.js +521 -0
  126. package/dist/psql/wire/protocol.js +466 -0
  127. package/dist/psql/wire/sasl.js +296 -0
  128. package/dist/psql/wire/tls.js +602 -0
  129. package/dist/storage_api.js +147 -0
  130. package/dist/test_utils/fixtures.js +122 -0
  131. package/dist/test_utils/oauth_server.js +9 -0
  132. package/dist/types.js +1 -0
  133. package/dist/utils/api_enums.js +33 -0
  134. package/dist/utils/auth.js +5 -0
  135. package/dist/utils/branch_notice.js +22 -0
  136. package/dist/utils/branch_picker.js +103 -0
  137. package/dist/utils/compute_units.js +28 -0
  138. package/dist/utils/enrichers.js +161 -0
  139. package/dist/utils/esbuild.js +158 -0
  140. package/dist/utils/formats.js +18 -0
  141. package/dist/utils/middlewares.js +20 -0
  142. package/dist/utils/package_manager.js +68 -0
  143. package/dist/utils/point_in_time.js +56 -0
  144. package/dist/utils/psql.js +120 -0
  145. package/dist/utils/string.js +5 -0
  146. package/dist/utils/ui.js +59 -0
  147. package/dist/utils/zip.js +4 -0
  148. package/dist/writer.js +97 -0
  149. package/package.json +117 -14
  150. package/.jshintrc +0 -3
  151. package/CHANGELOG.markdown +0 -22
  152. package/LICENSE +0 -21
  153. package/README.markdown +0 -76
  154. package/bower.json +0 -23
  155. package/license.txt +0 -9
  156. package/neon.js +0 -234
  157. package/stdlib/bubbling_support.js +0 -32
  158. package/stdlib/custom_event.js +0 -54
  159. package/stdlib/custom_event_support.js +0 -195
  160. package/stdlib/index.js +0 -7
  161. package/stdlib/node_support.js +0 -123
  162. package/stdlib/widget.js +0 -340
  163. package/test/neon_browser.html +0 -11
  164. package/test/neon_stdlib_browser.html +0 -15
  165. package/test/neon_stdlib_test.js +0 -84
  166. package/test/neon_test.js +0 -64
package/README.md ADDED
@@ -0,0 +1,601 @@
1
+ The Neon CLI is a command-line interface that lets you manage [Neon Serverless Postgres](https://neon.tech/) directly from the terminal. For the complete documentation, see [Neon CLI](https://neon.tech/docs/reference/neon-cli).
2
+
3
+ ## Install the Neon CLI
4
+
5
+ **npm**
6
+
7
+ ```shell
8
+ npm i -g neonctl
9
+ ```
10
+
11
+ Requires Node.js 18.0 or higher.
12
+
13
+ **Howebrew**
14
+
15
+ ```shell
16
+ brew install neonctl
17
+ ```
18
+
19
+ **Binary (macOS, Linux, Windows)**
20
+
21
+ Download a binary file [here](https://github.com/neondatabase/neonctl/releases).
22
+
23
+ ### Upgrade
24
+
25
+ **npm**
26
+
27
+ ```shell
28
+ npm update -g neonctl
29
+ ```
30
+
31
+ Requires Node.js 18.0 or higher.
32
+
33
+ **Howebrew**
34
+
35
+ ```shell
36
+ brew upgrade neonctl
37
+ ```
38
+
39
+ **Binary (macOS, Linux, Windows)**
40
+
41
+ To upgrade a binary version, download the latest binary file, as described above, and replace your old binary with the new one.
42
+
43
+ ## Connect
44
+
45
+ Run the following command to authenticate a connection to Neon:
46
+
47
+ ```bash
48
+ neonctl auth
49
+ ```
50
+
51
+ The `auth` command launches a browser window where you can authorize the Neon CLI to access your Neon account. Running a Neon CLI command without authenticating with [neonctl auth](https://neon.tech/docs/reference/cli-auth) automatically launches the browser authentication process.
52
+
53
+ Alternatively, you can authenticate a connection with a Neon API key using the `--api-key` option when running a Neon CLI command. For example, an API key is used with the following `neonctl projects list` command:
54
+
55
+ ```bash
56
+ neonctl projects list --api-key <neon_api_key>
57
+ ```
58
+
59
+ For information about obtaining an Neon API key, see [Authentication](https://api-docs.neon.tech/reference/authentication), in the _Neon API Reference_.
60
+
61
+ ## Connect with psql
62
+
63
+ ### The `psql` command
64
+
65
+ `neonctl psql [branch]` opens a psql session against a branch. It builds the connection string for the branch and launches psql — a shortcut for `neonctl connection-string --psql`. See [Neon CLI commands — psql](https://neon.com/docs/reference/cli-psql) for the full reference.
66
+
67
+ ```bash
68
+ neonctl psql # default branch
69
+ neonctl psql main # a specific branch
70
+ neonctl psql main@2024-01-01T00:00:00Z # point-in-time (branch@timestamp or branch@lsn)
71
+ neonctl psql --pooled # use the pooled connection
72
+ ```
73
+
74
+ Arguments after `--` are forwarded to psql:
75
+
76
+ ```bash
77
+ neonctl psql main -- -c "SELECT version()"
78
+ neonctl psql main -- -f script.sql --csv
79
+ ```
80
+
81
+ Options: `--project-id`, `--role-name`, `--database-name`, `--pooled`, `--endpoint-type` (`read_only` | `read_write`), `--ssl`, plus the [global options](#global-options).
82
+
83
+ ### The `--psql` flag
84
+
85
+ Several other commands accept a `--psql` flag that opens a psql session against the resolved endpoint:
86
+
87
+ ```bash
88
+ neonctl connection-string --psql --project-id <id>
89
+ neonctl projects create --psql
90
+ neonctl branches create --psql
91
+ ```
92
+
93
+ Any arguments after `--` are forwarded to psql, for example:
94
+
95
+ ```bash
96
+ neonctl cs --psql --project-id <id> -- -c "SELECT version()"
97
+ neonctl cs --psql --project-id <id> -- -f script.sql --csv
98
+ ```
99
+
100
+ ### Embedded psql fallback
101
+
102
+ If the system has `psql` installed on `$PATH`, `--psql` continues to spawn the native binary — there is no behavior change for existing users.
103
+
104
+ If `psql` is not found on `$PATH`, neonctl now falls back to an embedded TypeScript implementation. There is nothing to install or configure; it ships with `neonctl`. This removes the "no psql binary" trap on machines (and CI runners) that don't have PostgreSQL client tools installed.
105
+
106
+ Automatic fallback is the intended path — there is normally no flag to set. The embedded implementation can also be force-selected (primarily for tests and CI, e.g. to exercise it even when a native `psql` is present):
107
+
108
+ - `--fallback` — force the embedded implementation on `connection-string`, `projects create`, and `branches create`. Intentionally hidden from `--help`: it's a test/CI knob, not a user-facing option (the automatic fallback above is the supported behavior).
109
+ - `NEONCTL_PSQL_FALLBACK=1` — environment variable with the same effect as `--fallback`. Convenient for scripts and CI.
110
+
111
+ The embedded implementation is verified against a conformance suite that
112
+ diffs its behavior against real PostgreSQL (14–18) and the upstream psql
113
+ regression + TAP tests.
114
+
115
+ #### What works
116
+
117
+ **REPL & scripting**
118
+
119
+ - Interactive REPL with a hand-rolled VT100 line editor (no native bindings); vi and emacs edit modes (`VI_MODE` psql variable)
120
+ - Persistent command history (`~/.psql_history`, libreadline format)
121
+ - `~/.psqlrc` autoload (including `$PGSYSCONFDIR/psqlrc` and version-suffixed variants)
122
+ - Scripted modes: `-c "SQL"`, `-f script.sql`, and stdin; `--single-transaction`, `ON_ERROR_STOP`, `ECHO`, `--echo-all`
123
+ - `SINGLELINE` (`-S`), `\timing`, `\watch` (named flags `c=`/`i=`/`m=`, unbounded continuous mode)
124
+
125
+ **Backslash commands**
126
+
127
+ - All output formats: aligned, unaligned, wrapped, csv, json, html, asciidoc, latex, latex-longtable, troff-ms (`\a \H \t \x \pset \f \C` …)
128
+ - All `\d*` describe commands with full upstream parity (columns, indexes, foreign keys, triggers, view definitions, sequences, RLS, replica identity, partitions, tablespaces, access methods, inheritance, FDW, stats objects, publications, subscriptions, per-column FDW options, TOAST owner)
129
+ - `\copy` to/from file, `PROGRAM`, `STDIN`, `STDOUT` (incl. the `\.` EOF marker); `\g` / `\gx` / `\gset` / `\gdesc` / `\gexec` and `\g | program` pipes
130
+ - Extended query + pipeline mode (`\bind`, `\bind_named`, `\startpipeline`, `\parse`, `\sendpipeline`)
131
+ - `\crosstabview`, `\lo_*` large objects, `\e`/`\edit` (external editor), `\s` (history), `\?`/`\h` help, `\if`/`\elif`/`\else`/`\endif`, `\set`/`\unset`, `\connect`, `\encoding` (live `SET client_encoding`), `\!`, `\cd`, `\prompt` (incl. no-echo `-`), `\password`
132
+ - Tab completion (~88 rules incl. live `pg_settings` GUC lookup, deep `ALTER` sub-actions, `JOIN` clauses, window `OVER`)
133
+
134
+ **Connection & authentication**
135
+
136
+ - libpq-equivalent lookup precedence: argv flags > URI > `PG*` env vars > `~/.pgpass` > `pg_service.conf` > libpq defaults
137
+ - SCRAM-SHA-256 / SCRAM-SHA-256-PLUS with `tls-server-end-point` channel binding (`channel_binding`); MD5 and cleartext; `require_auth`
138
+ - Multi-host failover & load balancing: `target_session_attrs` (any / read-write / read-only / primary / standby / prefer-standby), `load_balance_hosts`, DNS fan-out, `hostaddr`
139
+ - Unix-domain sockets (host beginning with `/`); TCP keepalives (`keepalives`, `keepalives_idle`)
140
+
141
+ **TLS**
142
+
143
+ - `sslmode` disable → verify-full; client certs in **PEM or DER** via `sslcert` / `sslkey` (+ `sslpassword` for encrypted keys, with the libpq group/world-readable-key check)
144
+ - Trust config: `sslrootcert` (incl. `=system` with `SSL_CERT_FILE` / `SSL_CERT_DIR`), default client-cert discovery (`~/.postgresql/postgresql.{crt,key}`), `sslcertmode`
145
+ - CRL: `sslcrl` and `sslcrldir`; `ssl_min_protocol_version` / `ssl_max_protocol_version`; `sslsni`
146
+ - Direct-SSL negotiation (`sslnegotiation=direct`, PostgreSQL 17+, via ALPN)
147
+
148
+ #### What's not supported
149
+
150
+ - **GSSAPI / SSPI** (`gssencmode`, Kerberos/SSPI auth, `requirepeer`). GSS transport encryption needs a native Kerberos binding, which the embedded psql deliberately avoids (pure TypeScript, zero native dependencies — the same reason the line editor is hand-rolled). `node-postgres` doesn't support it either, and Neon doesn't use it. `gssencmode=disable` / `prefer` are accepted; `gssencmode=require` is rejected with a clear error. `requirepeer` is parsed but a Unix-socket connection that sets it is refused (Node exposes no peer-credential API — it is not silently ignored).
151
+ - **`keepalives_interval` / `keepalives_count`** — Node's socket API exposes only keepalive enable + initial delay, so these are accepted but not applied.
152
+
153
+ ### Known limitations
154
+
155
+ - **TLS cipher is runtime-dependent.** The negotiated TLS 1.3 ciphersuite is chosen by the host runtime's TLS library from an offer byte-identical to libpq's. Under Node (OpenSSL) that is `TLS_AES_256_GCM_SHA384`, matching vanilla psql; under Bun (BoringSSL) it is `TLS_AES_128_GCM_SHA256`. Both are TLS 1.3 AEAD suites with no practical security difference, and neither runtime exposes a client-side knob to steer the selection.
156
+
157
+ ## Configure autocompletion
158
+
159
+ The Neon CLI supports autocompletion, which you can configure in a few easy steps. See [Neon CLI commands — completion](https://neon.tech/docs/reference/cli-completion) for instructions.
160
+
161
+ ## Linking a project
162
+
163
+ `neonctl link` is a Vercel-style command that binds the current directory to a Neon project. It picks (or creates) an organization and a project and writes a `.neon` file (`{ "orgId", "projectId", "branch" }`) that subsequent commands run in this directory (or any sub-directory) pick up automatically.
164
+
165
+ `link` resolves what it can and **verifies every identifier you pass** before writing, so a `.neon` is never left half-written or pointing at something that doesn't exist:
166
+
167
+ - **org** is inferred from the project (so `--project-id` alone is enough); it's omitted only when the project has no organization (personal account).
168
+ - **project** is taken from `--project-id` (or chosen interactively / via `--agent`).
169
+ - **branch** is left to an explicit [`neonctl checkout <branch>`](#checkout) — `link` never silently pins a project's default branch (that would make later commands quietly target, say, production). It only records a branch when you pass `--branch`, when one is already pinned for the same project (preserved), when you pick one in the interactive picker, or for a freshly **created** project (whose single branch is unambiguous).
170
+
171
+ When a branch ends up pinned, `link` also runs [`env pull`](#env-pull) so the branch's Neon env vars (`DATABASE_URL`, …) land in a local `.env`. With no branch pinned there is nothing to pull, so `link` instead nudges you to run `neonctl checkout`. Pass `--no-env-pull` to skip the pull (for example when injecting env at runtime with `neon-env run` or `neonctl dev`).
172
+
173
+ > **Migrating from `set-context`?** `set-context` is **deprecated** in favor of `link` (see [below](#set-context-is-deprecated)). It still works exactly as before for now (a raw write), it just prints a deprecation warning. The `.neon` `branchId` field is also superseded by `branch` (which stores the branch **name** when known); old `branchId` files are still read and are upgraded to `branch` the next time `link`/`checkout` writes the context.
174
+
175
+ There are three modes:
176
+
177
+ **Interactive (default)** — guided prompts for humans:
178
+
179
+ ```bash
180
+ $ neonctl link
181
+ ? Which organization would you like to link? › Personal Org (org-abc123)
182
+ ? Which project would you like to link? › + Create new project…
183
+ ? Name for the new project: › my-app
184
+ ? Which region should the new project run in? › AWS US East (Ohio) (aws-us-east-2)
185
+ Created project polished-snowflake-12345678 ("my-app") in aws-us-east-2.
186
+ Linked .neon:
187
+ orgId: org-abc123
188
+ projectId: polished-snowflake-12345678
189
+ branch: main
190
+ ```
191
+
192
+ When you link an **existing** project that has more than one branch, the interactive flow adds a
193
+ final step to pick which branch to pin — the same `+ Create a new branch…` + list selector used by
194
+ `neonctl checkout` (a single-branch project is pinned automatically, no prompt). Non-interactive
195
+ `link --project-id …` does **not** prompt or default a branch; it links org + project and leaves
196
+ branch selection to `neonctl checkout`:
197
+
198
+ ```bash
199
+ ? Which organization would you like to link? › Personal Org (org-abc123)
200
+ ? Which project would you like to link? › my-app (polished-snowflake-12345678)
201
+ ? Which branch would you like to link? › [default] main (br-main-branch-87654321)
202
+ ```
203
+
204
+ **Non-interactive (flags or `--params` JSON)** — for scripts and CI:
205
+
206
+ ```bash
207
+ # Link to an existing project (org is inferred from the project; no branch pinned)
208
+ neonctl link --project-id polished-snowflake-12345678
209
+
210
+ # Same, but also pin a branch (name or id — resolved and stored as its name)
211
+ neonctl link --project-id polished-snowflake-12345678 --branch main
212
+
213
+ # Pin/switch the branch in the already-linked project
214
+ neonctl link --branch main # alias: --branch-id
215
+
216
+ # Create a new project and link it (pins the new project's default branch)
217
+ neonctl link --org-id org-abc123 --project-name my-app --region-id aws-us-east-2
218
+
219
+ # Same payload, one JSON blob
220
+ neonctl link --params '{"orgId":"org-abc123","projectName":"my-app","regionId":"aws-us-east-2"}'
221
+
222
+ # Record just the default org (preserves any existing project/branch)
223
+ neonctl link --org-id org-abc123
224
+
225
+ # Forget the current context
226
+ neonctl link --clear
227
+
228
+ # Offline write — no API calls, no verification (see --no-checks below)
229
+ neonctl link --no-checks --org-id org-abc123 --project-id polished-snowflake-12345678
230
+ ```
231
+
232
+ Every supplied identifier is checked before anything is written, with actionable errors — e.g. `Project '…' not found`, `You don't have access to project '…'`, `Organization '…' not found, or your API key doesn't have access to it`, `Project '…' belongs to organization 'A', not 'B'`, or `Branch '…' not found in project '…'. Available branches: …`.
233
+
234
+ **Agent mode (`--agent`)** — a JSON state machine designed for AI coding assistants. Each invocation returns a single JSON object with a `status` discriminator describing the next step, the available options, and the exact follow-up command to run.
235
+
236
+ ```bash
237
+ $ neonctl link --agent
238
+ {
239
+ "status": "needs_org",
240
+ "instruction": "Ask the user which of these 2 organizations they want to link the current directory to. After they pick one, re-run the next_command_template with the chosen --org-id value.",
241
+ "options": [
242
+ { "id": "org-abc123", "name": "Personal Org" },
243
+ { "id": "org-team", "name": "Team Org" }
244
+ ],
245
+ "next_command_template": "neonctl link --agent --org-id <org_id>"
246
+ }
247
+
248
+ $ neonctl link --agent --org-id org-abc123
249
+ {
250
+ "status": "needs_project",
251
+ "instruction": "Ask the user whether to link to one of these 1 existing projects (use next_command_template with --project-id) or create a new project (use create_option.next_command_template).",
252
+ "options": [
253
+ { "id": "polished-snowflake-12345678", "name": "my-app" }
254
+ ],
255
+ "create_option": {
256
+ "instruction": "To create a new project, ask the user for a project name. The region can be omitted to receive a follow-up needs_project_details response that lists available regions.",
257
+ "next_command_template": "neonctl link --agent --org-id org-abc123 --project-name <name> --region-id <region_id>"
258
+ },
259
+ "next_command_template": "neonctl link --agent --org-id org-abc123 --project-id <project_id>"
260
+ }
261
+
262
+ $ neonctl link --agent --org-id org-abc123 --project-id polished-snowflake-12345678
263
+ {
264
+ "status": "linked",
265
+ "context_file": "/path/to/cwd/.neon",
266
+ "context": {
267
+ "orgId": "org-abc123",
268
+ "projectId": "polished-snowflake-12345678"
269
+ },
270
+ "project": { "id": "polished-snowflake-12345678" },
271
+ "message": "Linked /path/to/cwd/.neon to project polished-snowflake-12345678 (org org-abc123). No branch pinned — run `neonctl checkout <branch>` (omit the branch to list options) to pin one and pull its env vars."
272
+ }
273
+ ```
274
+
275
+ The `linked` response omits `branch` unless one was pinned (via `--branch`, an existing pin, or project creation); pass `--branch <name|id>` to include it. The agent flow also handles project creation: if the agent sends `--project-name` without `--region-id`, the next response is `needs_project_details` with the list of supported regions.
276
+
277
+ **Organization-scoped API keys** (those created at the organization level rather than the user level) cannot list user organizations or call the regions endpoint. `link` handles this transparently:
278
+
279
+ - If the API key is org-scoped and at least one project already exists in the org, the CLI auto-detects the `org_id` from the first project. In interactive mode it prints an informational message; in `--agent` mode it skips straight to `needs_project`.
280
+ - If the API key is org-scoped and no projects exist yet, `--agent` returns a `needs_org` response with `options: []` and an instruction telling the user to find their org ID in the Neon Console. Interactive mode prints an error pointing to `--org-id`.
281
+ - When the regions endpoint is not allowed, `link` falls back to a built-in static region list.
282
+
283
+ **Agent error contract**: any unexpected failure in `--agent` mode is reported as JSON to stdout with exit code 1, so agents can always parse the response:
284
+
285
+ ```json
286
+ {
287
+ "status": "error",
288
+ "code": "CLIENT_ERROR",
289
+ "message": "user has no access to projects"
290
+ }
291
+ ```
292
+
293
+ **Offline writes (`--no-checks`)** — write the `.neon` with no API calls at all: no org inference, no existence/access verification, no env pull. Because nothing can be resolved offline, it requires both `--org-id` and `--project-id` (`--branch` optional, stored verbatim). Handy for scripted/CI setups or re-creating a `.neon` from values you already trust:
294
+
295
+ ```bash
296
+ neonctl link --no-checks --org-id org-abc123 --project-id polished-snowflake-12345678 --branch main
297
+ ```
298
+
299
+ #### `set-context` is deprecated
300
+
301
+ `set-context` is **deprecated** in favor of `link` and prints a deprecation warning (to stderr, so it never pollutes stdout or scripts). For backward compatibility its behavior is **unchanged**: it's still a raw, offline write of exactly the fields you pass (no org inference, no verification, no env pull), and bare `set-context` still clears the file. Nothing breaks today — but new work should use `link`, and `set-context` will be removed in a future major release.
302
+
303
+ How today's `set-context` uses map onto `link`:
304
+
305
+ | `set-context` (deprecated) | Recommended `link` equivalent |
306
+ | --------------------------------------- | ----------------------------------------------------------------------------- |
307
+ | `neonctl set-context --project-id <id>` | `neonctl link --project-id <id>` (infers org + verifies; branch via checkout) |
308
+ | `neonctl set-context --org-id <id>` | `neonctl link --org-id <id>` |
309
+ | `neonctl set-context --branch-id <id>` | `neonctl link --branch <name\|id>` |
310
+ | `neonctl set-context` (clear) | `neonctl link --clear` |
311
+ | a raw local write (no network) | `neonctl link --no-checks --org-id <id> --project-id <id>` |
312
+
313
+ The key difference: `link` resolves and **verifies** before writing (so you never get a half-written or stale `.neon`), whereas `set-context` writes whatever you give it verbatim. The closest like-for-like replacement for the old raw write is `link --no-checks`.
314
+
315
+ ### checkout
316
+
317
+ `checkout [id|name]` pins a branch in the local context so subsequent commands target it — it's the focused companion to `link` for the common "switch the branch I'm working on" case (`link` resolves org + project; `checkout` pins the branch). It resolves the branch (by name or id) against the project, then **heals** the `.neon` file: it always (re)writes `projectId`, `branch`, and `orgId` (when the project has one), so a `.neon` that was missing fields or drifted ends up complete and consistent. The branch is stored as its **name** when known (matching `link`). When `orgId` isn't already known (from `--org-id` or the existing `.neon`), it's looked up from the project itself.
318
+
319
+ The branch argument is **optional**: run `neonctl checkout` with no branch in an interactive terminal to fetch the project's branches and pick one from a list. In a non-interactive context (CI or no TTY), a branch must be passed explicitly.
320
+
321
+ Branch **id vs name** is detected automatically (a `br-…` value is treated as an id):
322
+
323
+ - **id** — matched strictly by id. A non-existent id is a hard "not found" error (ids are server-assigned, so checkout never creates one).
324
+ - **name** — matched by name. If the name doesn't exist, in an interactive terminal `checkout` offers to **create** it (equivalent to `neonctl branch create --name <name>`: branched from the project's default branch with a read-write compute), then checks it out. In a non-interactive context a missing name is the usual "not found" error.
325
+
326
+ The project is resolved through the standard neonctl chain, each entry winning over the next:
327
+
328
+ 1. `--project-id <id>` flag
329
+ 2. `projectId` from the closest `.neon` file (found by walking up from the current directory — see "Where `.neon` lives" below)
330
+ 3. If still unresolved and the API key maps to exactly one project, that project is auto-detected (same behaviour as `branches` and `connection-string`)
331
+
332
+ If none of those resolve a project, `checkout` prints a telling error explaining the chain above. In an interactive terminal it then offers to run `neonctl link` in the current folder so you can pick (or create) a project on the spot; once linked, it continues and pins the requested branch. In non-interactive contexts (CI or no TTY) it exits with a non-zero code and the same guidance instead of prompting.
333
+
334
+ The resolved branch is then written (by name) to the same `.neon` file `link` uses:
335
+
336
+ ```bash
337
+ $ neonctl checkout main --project-id polished-snowflake-12345678
338
+ INFO: Checked out branch br-main-branch-87654321 on project polished-snowflake-12345678. Updated /path/to/cwd/.neon.
339
+
340
+ $ cat .neon
341
+ {
342
+ "orgId": "org-abc123",
343
+ "projectId": "polished-snowflake-12345678",
344
+ "branch": "main"
345
+ }
346
+ ```
347
+
348
+ After pinning the branch, `checkout` also runs [`env pull`](#env-pull) by default, so the branch's Neon env vars are written to your local `.env` and you can start building right away — the branch-first loop is just `link` + `checkout`. Pass `--no-env-pull` to skip it (for example when env is injected at runtime via `neon-env run` / `neonctl dev`, or to keep secrets out of the working tree). A pull failure never undoes the checkout: the branch stays pinned and the failure is surfaced as a warning pointing you at `neonctl env pull` (or `neonctl deploy` if a `neon.ts`-declared service is missing).
349
+
350
+ ### env pull
351
+
352
+ `env pull` writes the linked branch's Neon environment variables into a local dotenv file: an existing `.env` if you have one, otherwise `.env.local` (override with `--file <path>`). Only Neon-managed keys (`DATABASE_URL`, `DATABASE_URL_UNPOOLED`, and the Neon Auth / Data API URLs when those services are enabled) are written; any other lines in the file are preserved. The branch comes from the closest `.neon` file, so no `--branch` is needed (pass `--branch <id|name>` to target another branch).
353
+
354
+ `link` and `checkout` invoke `env pull` automatically (see above), so you usually only run it by hand to refresh vars or to pull a different branch into a specific file:
355
+
356
+ ```bash
357
+ # Refresh the linked branch's vars in place
358
+ neonctl env pull
359
+
360
+ # Pull a specific branch into a specific file
361
+ neonctl env pull --branch preview --file .env.preview
362
+ ```
363
+
364
+ If you'd rather not keep env vars on disk, inject them at runtime instead with `neon-env run -- <your dev command>` (from `@neon/env`) or `neonctl dev`, and pass `--no-env-pull` to `link` / `checkout`.
365
+
366
+ **Where `.neon` lives**: `link` writes `.neon` into the **current working directory** by default. If an existing `.neon` is found in any parent directory, that file is reused — so commands run from a sub-directory of a linked project still pick up the project's context. To pin the location explicitly, pass `--context-file <path>`.
367
+
368
+ **`.gitignore` scaffolding**: when `.neon` is **created** for the first time, the CLI also makes sure a `.gitignore` sits alongside it listing `.neon`. If `.gitignore` doesn't exist it's created with a single `.neon` line; if it does exist, `.neon` is appended only when missing (no duplicates, your other entries are left alone). On subsequent updates to an existing `.neon`, `.gitignore` is left untouched — so if you deliberately un-ignore `.neon` (e.g. to commit shared context), the entry is not re-added on every command.
369
+
370
+ ## Config as code (`config` / `deploy`)
371
+
372
+ Describe a branch's desired state in a `neon.ts` policy and reconcile it from the CLI — the Neon equivalent of `terraform status` / `plan` / `apply`. A policy splits into a **static** existential set — top-level `auth` / `dataApi` toggles and the beta `preview` block (Functions, buckets, AI Gateway) that decide what _exists_ — and a **dynamic** `branch` closure that tunes each branch (compute settings, TTL, protection, `parent`) based on the branch it's evaluated for (`name`, `isDefault`, …):
373
+
374
+ ```ts
375
+ // neon.ts
376
+ import { defineConfig } from '@neon/config/v1';
377
+
378
+ export default defineConfig({
379
+ // Static: what exists on every branch (drives the typed env).
380
+ auth: true,
381
+ // Dynamic: per-branch tuning only — cannot add/remove services.
382
+ branch: (branch) => {
383
+ if (branch.isDefault) {
384
+ return { protected: true };
385
+ }
386
+ return { parent: 'main', ttl: '7d' };
387
+ },
388
+ });
389
+ ```
390
+
391
+ Three sub-commands plus two top-level aliases drive it:
392
+
393
+ ```bash
394
+ # Inspect the branch's live Neon state (read-only — never mutates)
395
+ neon config status
396
+
397
+ # `neon status` is an alias for `neon config status`
398
+ neon status
399
+
400
+ # Dry-run diff: show exactly what `apply` would change
401
+ neon config plan
402
+
403
+ # Reconcile the policy against the branch
404
+ neon config apply
405
+
406
+ # `neon deploy` is an alias for `neon config apply`
407
+ neon deploy
408
+ ```
409
+
410
+ **Project & branch resolution** follows the same chain as the rest of the CLI, each entry winning over the next:
411
+
412
+ 1. `--project-id <id>` flag
413
+ 2. `projectId` from the closest `.neon` file (found by walking up from the current directory — see "Where `.neon` lives" above)
414
+ 3. If still unresolved and the API key maps to exactly one project, that project is auto-detected
415
+
416
+ The branch is chosen with `--branch <id|name>`; without it the project's default branch is used. The policy itself is found by walking up from the current directory for a `neon.ts`, or pass `--config <path>` to point at one explicitly.
417
+
418
+ **Apply-only flags** (also available on `deploy`):
419
+
420
+ - `--update-existing` — auto-confirm overriding existing remote settings on the branch. Without it, drift on settings already present remotely (compute, TTL, `protected`) is reported as a **conflict** and `apply` makes no changes until you resolve it or pass this flag.
421
+ - `--allow-protected` — auto-confirm applying to a branch Neon marks as protected. Without it, `apply` refuses to touch a protected branch.
422
+
423
+ **Output**: `status` prints the project, branch, and reverse-engineered config; `plan` / `apply` print the planned/applied changes and any conflicts as tables. Pass `--output json` (or `--output yaml`) to emit the full machine-readable result (`PushResult`) for piping into other tools or CI.
424
+
425
+ **`config status --current-branch`** (alias `neon status --current-branch`) prints _only_ the branch pinned in the local `.neon` file — no network, no auth, no analytics — and exits non-zero when none is pinned. This behavior lets it safely drive a shell prompt. Example [starship](https://starship.rs) segment:
426
+
427
+ ```toml
428
+ [custom.neon]
429
+ description = "Current Neon database branch"
430
+ format = "[$symbol$output]($style) "
431
+ style = "bold green"
432
+ # `symbol` below uses a Nerd Font glyph; swap it for a plain
433
+ # label/emoji if you don't have a Nerd Font installed.
434
+ symbol = " "
435
+ command = "neon status --current-branch"
436
+ # Starship evaluates this on EVERY prompt render. To keep prompts instant
437
+ # everywhere outside a Neon project, do a zero-subprocess walk-up for an
438
+ # ancestor `.neon` first (the same walk the CLI does, stopping at $HOME and /).
439
+ # Only when one is found do we invoke the CLI, whose exit code is the real
440
+ # gate: non-zero (no branch pinned) hides the segment cleanly.
441
+ when = '''
442
+ d="$PWD"
443
+ while [ "$d" != "$HOME" ] && [ "$d" != / ]; do
444
+ if [ -e "$d/.neon" ]; then
445
+ neon status --current-branch >/dev/null 2>&1
446
+ exit $?
447
+ fi
448
+ d=$(dirname "$d")
449
+ done
450
+ exit 1
451
+ '''
452
+ ```
453
+
454
+ ```bash
455
+ # CI gate: fail the build if the branch has drifted from the policy
456
+ neon config plan --project-id polished-snowflake-12345678 --output json
457
+
458
+ # Reconcile a feature branch, overriding any manual tweaks made in the console
459
+ neon deploy --branch my-feature --update-existing
460
+ ```
461
+
462
+ Function deploys declared under `preview.functions` are bundled by neonctl's own esbuild helper and uploaded as part of `apply`, so the policy stays declarative and the packaged CLI never has to embed esbuild's native binary.
463
+
464
+ ## Scaffold a project (`bootstrap`)
465
+
466
+ `neonctl bootstrap` copies a Neon starter template into a new (or current) directory — conceptually like `degit`, but it only pulls from a small set of templates we maintain in the public [`neondatabase/examples`](https://github.com/neondatabase/examples) repo. It requires no Neon login: it just downloads files from GitHub.
467
+
468
+ Pass a target directory (or `.` for the current one). In an interactive terminal you pick the template from a list; in CI / non-interactive contexts pass `--template <id>`.
469
+
470
+ ```bash
471
+ # Pick a template interactively and scaffold it into ./my-app
472
+ $ neonctl bootstrap my-app
473
+
474
+ # Scaffold a specific template into the current directory (no prompts)
475
+ $ neonctl bootstrap . --template hono
476
+ ```
477
+
478
+ The target directory must be empty unless you pass `--force` (a lone `.git` is ignored, so a freshly `git init`ed folder is fine). Symlinks and executable bits in the template are preserved.
479
+
480
+ ## Commands
481
+
482
+ | Command | Subcommands | Description |
483
+ | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ---------------------------------- |
484
+ | [auth](https://neon.com/docs/reference/cli-auth) | | Authenticate |
485
+ | [projects](https://neon.com/docs/reference/cli-projects) | `list`, `create`, `update`, `delete`, `get` | Manage projects |
486
+ | [ip-allow](https://neon.com/docs/reference/cli-ip-allow) | `list`, `add`, `remove`, `reset` | Manage IP Allow |
487
+ | [me](https://neon.com/docs/reference/cli-me) | | Show current user |
488
+ | [branches](https://neon.com/docs/reference/cli-branches) | `list`, `create`, `rename`, `add-compute`, `set-default`, `set-expiration`, `delete`, `get` | Manage branches |
489
+ | [databases](https://neon.com/docs/reference/cli-databases) | `list`, `create`, `delete` | Manage databases |
490
+ | function | `deploy`, `list`, `get`, `delete` | Manage Neon Functions |
491
+ | [roles](https://neon.com/docs/reference/cli-roles) | `list`, `create`, `delete` | Manage roles |
492
+ | [operations](https://neon.com/docs/reference/cli-operations) | `list` | Manage operations |
493
+ | [connection-string](https://neon.com/docs/reference/cli-connection-string) | | Get connection string |
494
+ | [psql](https://neon.com/docs/reference/cli-psql) | | Connect to a database via psql |
495
+ | set-context | | Deprecated; use `link` |
496
+ | env | `pull` | Manage a branch's env vars |
497
+ | checkout | | Pin a branch in `.neon` |
498
+ | [link](https://neon.com/docs/reference/cli-link) | | Link a directory to a project |
499
+ | config | `status`, `plan`, `apply` | Drive a branch from `neon.ts` |
500
+ | deploy | | Alias for `config apply` |
501
+ | bootstrap | | Scaffold a project from a template |
502
+ | bucket | `create`, `list`, `delete`, `object list`, `object get`, `object put`, `object delete` (incl. `--recursive`) | Manage buckets and their objects |
503
+ | [completion](https://neon.com/docs/reference/cli-completion) | | Generate a completion script |
504
+
505
+ ## Global options
506
+
507
+ Global options are supported with any Neon CLI command.
508
+
509
+ | Option | Description | Type | Default |
510
+ | :-------------------------- | :---------------------------------------------------------- | :------ | :----------------------------- |
511
+ | [-o, --output](#output) | Set the Neon CLI output format (`json`, `yaml`, or `table`) | string | table |
512
+ | [--config-dir](#config-dir) | Path to the Neon CLI configuration directory | string | `/home/<user>/.config/neonctl` |
513
+ | [--api-key](#api-key) | Neon API key | string | "" |
514
+ | [--analytics](#analytics) | Manage analytics | boolean | true |
515
+ | [-v, --version](#version) | Show the Neon CLI version number | boolean | - |
516
+ | [-h, --help](#help) | Show the Neon CLI help | boolean | - |
517
+
518
+ - <a id="output"></a>`-o, --output`
519
+
520
+ Sets the output format. Supported options are `json`, `yaml`, and `table`. The default is `table`. Table output may be limited. The `json` and `yaml` output formats show all data.
521
+
522
+ ```bash
523
+ neonctl me --output json
524
+ ```
525
+
526
+ - <a id="config-dir"></a>`--config-dir`
527
+
528
+ Specifies the path to the `neonctl` configuration directory. To view the default configuration directory containing you `credentials.json` file, run `neonctl --help`. The credentials file is created when you authenticate using the `neonctl auth` command. This option is only necessary if you move your `neonctl` configuration file to a location other than the default.
529
+
530
+ ```bash
531
+ neonctl projects list --config-dir /home/dtprice/.config/neonctl
532
+ ```
533
+
534
+ - <a id="api-key"></a>`--api-key`
535
+
536
+ Specifies your Neon API key. You can authenticate using a Neon API key when running a Neon CLI command instead of using `neonctl auth`. For information about obtaining an Neon API key, see [Authentication](https://api-docs.neon.tech/reference/authentication), in the _Neon API Reference_.
537
+
538
+ ```bash
539
+ neonctl <command> --api-key <neon_api_key>
540
+ ```
541
+
542
+ - <a id="analytics"></a>`--analytics`
543
+
544
+ Analytics are enabled by default to gather information about the CLI commands and options that are used by our customers. This data collection assists in offering support, and allows for a better understanding of typical usage patterns so that we can improve user experience. Neon does not collect user-defined data, such as project IDs or command payloads. To opt-out of analytics data collection, specify `--no-analytics` or `--analytics false`.
545
+
546
+ - <a id="version"></a>`-v, --version`
547
+
548
+ Shows the Neon CLI version number.
549
+
550
+ ```bash
551
+ $ neonctl --version
552
+ 1.15.0
553
+ ```
554
+
555
+ - <a id="help"></a>`-h, --help`
556
+
557
+ Shows the `neonctl` command-line help. You can view help for `neonctl`, a `neonctl` command, or a `neonctl` subcommand, as shown in the following examples:
558
+
559
+ ```bash
560
+ neonctl --help
561
+
562
+ neonctl branches --help
563
+
564
+ neonctl branches create --help
565
+ ```
566
+
567
+ ## Contribute
568
+
569
+ This repo uses [pnpm](https://pnpm.io). The required version is pinned in `.tool-versions` and `package.json`'s `packageManager` field. The simplest way to get the right version is [mise](https://mise.jdx.dev): `mise install` reads `.tool-versions` and installs Node and pnpm. Alternatives: `npm install -g pnpm@9.15.9`, or [Corepack](https://nodejs.org/api/corepack.html) (`corepack enable pnpm`).
570
+
571
+ To run the CLI locally, execute the build command after making changes:
572
+
573
+ ```shell
574
+ pnpm install
575
+ pnpm run build
576
+ ```
577
+
578
+ To develop continuously:
579
+
580
+ ```shell
581
+ pnpm run watch
582
+ ```
583
+
584
+ To run commands from the local build, replace the `neonctl` command with `node dist`; for example:
585
+
586
+ ```shell
587
+ node dist branches --help
588
+ ```
589
+
590
+ ### Embedded psql tests
591
+
592
+ The embedded TypeScript psql implementation has its own conformance test suite that runs the same scripts against the embedded psql and a reference `psql` binary, then diffs the output.
593
+
594
+ ```shell
595
+ bun run test:conformance # run against $PSQL_BINARY (defaults to the system psql)
596
+ bun run test:conformance:matrix # run across PG 14/15/16/17/18 locally (requires Docker)
597
+ ```
598
+
599
+ ## Releasing
600
+
601
+ Maintainers: see [`RELEASING.md`](./RELEASING.md) for the two-stage publish flow.