The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: Roles |
| 3 | description: Create Postgres roles on a Neon branch with Neon.Role — separate logins for your app and your migrations, with passwords as Redacted outputs. |
| 4 | --- |
| 5 | |
| 6 | Every Neon project starts with one owner role. Use `Neon.Role` to give |
| 7 | your app, your migrations, and your teammates their own logins. |
| 8 | |
| 9 | :::caution |
| 10 | Every role has the same access as the owner. It can read and write all |
| 11 | data, create databases and roles, and bypass row-level security. |
| 12 | ::: |
| 13 | |
| 14 | ## Create a login role |
| 15 | |
| 16 | Pass the branch the role lives on: |
| 17 | |
| 18 | ```typescript |
| 19 | const app = yield* Neon.Role("App", { branch }); |
| 20 | |
| 21 | app.roleName; // "myapp_app_dev_x7k2..." |
| 22 | app.password; // Redacted<string> |
| 23 | ``` |
| 24 | |
| 25 | The generated name uses underscores, so it works unquoted in SQL. The |
| 26 | password stays the same on every deploy and never prints in logs or plan |
| 27 | output. |
| 28 | |
| 29 | ## Name the role |
| 30 | |
| 31 | Set `name` when other code or SQL refers to the role: |
| 32 | |
| 33 | ```typescript |
| 34 | const migrator = yield* Neon.Role("Migrator", { branch, name: "migrator" }); |
| 35 | ``` |
| 36 | |
| 37 | Changing `name` replaces the role. The old role is deleted and you get a |
| 38 | new password. |
| 39 | |
| 40 | ## Use the project's default branch |
| 41 | |
| 42 | Pass `project` instead of `branch` to put the role on the default branch: |
| 43 | |
| 44 | ```typescript |
| 45 | const migrator = yield* Neon.Role("Migrator", { project, name: "migrator" }); |
| 46 | ``` |
| 47 | |
| 48 | ## Connect as the role |
| 49 | |
| 50 | Swap the role into the branch's origin. Here it feeds Hyperdrive: |
| 51 | |
| 52 | ```typescript |
| 53 | const origin = Output.all(branch.origin, app.roleName, app.password).pipe( |
| 54 | Output.map(([origin, user, password]) => ({ ...origin, user, password: password! })), |
| 55 | ); |
| 56 | |
| 57 | const hyperdrive = yield* Cloudflare.Hyperdrive.Connection("app-hyperdrive", { origin }); |
| 58 | ``` |
| 59 | |
| 60 | The host, port, and database come from the branch; the user and password |
| 61 | come from the role. |
| 62 | |
| 63 | ## Group roles |
| 64 | |
| 65 | Set `noLogin` for a role that owns objects but never connects: |
| 66 | |
| 67 | ```typescript |
| 68 | const owner = yield* Neon.Role("Owner", { |
| 69 | branch, |
| 70 | name: "app_owner", |
| 71 | noLogin: true, |
| 72 | }); |
| 73 | ``` |
| 74 | |
| 75 | Its `password` is `undefined`. Grant it to login roles with SQL so they can act as it: |
| 76 | |
| 77 | ```sql |
| 78 | GRANT app_owner TO migrator; |
| 79 | ``` |
| 80 | |
| 81 | Switching `noLogin` on or off replaces the role. |
| 82 | |
| 83 | ## Adopt an existing role |
| 84 | |
| 85 | A role created in the console is refused until you adopt it: |
| 86 | |
| 87 | ```typescript |
| 88 | const reporting = yield* Neon.Role("Reporting", { |
| 89 | project, |
| 90 | name: "reporting", |
| 91 | }).pipe(adopt(true)); |
| 92 | ``` |
| 93 | |
| 94 | `reporting.password` is the role's current password. If Neon no longer |
| 95 | stores it, the password is reset. |
| 96 | |
| 97 | :::note |
| 98 | Every prop and attribute is on the [Role API reference](/providers/neon/role). |
| 99 | For the branch's own connection outputs, see [Connections](/neon/data/connections). |
| 100 | ::: |