Skip to content

feat(stack): run native PostgreSQL as a non-root user when started as root - #6823

Merged
avallete merged 3 commits into
developfrom
avallete/postgres-root-fallback-938526
Sep 25, 2026
Merged

avallete merged 3 commits into
developfrom
avallete/postgres-root-fallback-938526

Conversation

@avallete

@avallete avallete commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Summary

TL;DR: supabase start --runtime native no longer fails immediately when it runs as root inside an agent sandbox. Only the PostgreSQL process steps down to a non-root user, so agents can start the native stack on the first try. Root outside a detected sandbox still fails fast unless SUPABASE_NATIVE_POSTGRES_USER names a user.

Before

flowchart LR
  A[native start as uid 0] --> B[fail: PostgreSQL cannot be run as root]
Loading

After

flowchart TD
  A[native start as uid 0] --> O{SUPABASE_NATIVE_POSTGRES_USER?}
  O -->|valid user| S[PostgreSQL runs as that user]
  O -->|unset| B{Claude Code sandbox?}
  B -->|no| E[fail fast, suggest the override]
  B -->|yes| P[ubuntu, else postgres,<br/>else lowest uid &gt;= 1000]
  P --> S
Loading

Why

Agent sandboxes such as Claude Code's cloud environment run as root by default and provide ordinary non-root accounts. The native runtime refused to start there with PostgreSQL cannot be run as root, so agents had to set up a separate user by hand before the stack could run.

What changed

  • A small resolver picks the PostgreSQL user: the SUPABASE_NATIVE_POSTGRES_USER override first, then a per-sandbox preferred user list (ubuntu for Claude Code, detected via CLAUDECODE and IS_SANDBOX=yes), then postgres, then the lowest uid at or above 1000. Accounts with uid 0, gid 0, or nobody are never used. Other agent sandboxes can be added as one table entry.
  • The native launcher spawns only PostgreSQL with that uid/gid, cwd /, and the user's HOME. Every other service keeps the stack's identity.
  • Before each stepped-down launch, the stack:
    • checks that every directory on the instance's real path is owned by root and writable only by root;
    • refuses files already owned by a different non-root user;
    • hands the data directory, root key, socket directory, and the cached bundle's pgsodium_getkey.sh to the user with chown -R -P;
    • adds traverse-only o+x to their parent directories, including root's home, logging each change.
  • The artifact cache root, artifact directories, and stack state root still restrict themselves to their owner on every use, but now keep an existing traverse-only grant instead of resetting to 0700. Without this, preparing another service after PostgreSQL launched cut the stepped-down server off from its own bundle and data directory: new sessions failed with could not access file "supautils" and config reloads could not read postgresql.conf.
  • start prints one line naming the user PostgreSQL runs as and why.
  • Database.prepare no longer refuses root, so supabase stack prepare works in root Dockerfiles.
  • Non-root users and the Docker runtime take the same path as before.
  • The package README and the start side-effects doc describe the new behavior and the environment variable.

Linked issue

None.

🤖 Generated with Claude Code

https://claude.ai/code/session_01REDom2C1Z1DUi2df7xGMMv

… root

Native PostgreSQL refuses uid 0, so agent sandboxes that run as root could
not start the native stack. When the stack runs as root inside a detected
Claude Code sandbox, or SUPABASE_NATIVE_POSTGRES_USER names a non-root user,
only the PostgreSQL process now runs as that user. The stack verifies the
instance path is root-controlled, hands the data directory, key, socket, and
the bundle's getkey script to the user, and adds traverse-only access on
their parent directories, logging each change. Root elsewhere still fails
fast, and `stack prepare` no longer refuses root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@avallete
avallete requested a review from a team as a code owner September 25, 2026 09:07
…root-fallback-938526

# Conflicts:
#	packages/stack/src/runtime/NativeProcess.ts
#	packages/stack/src/runtime/native-launcher.ts
#	packages/stack/src/services/Database.ts
@avallete avallete added the run-preview-packages Publish pkg.pr.new preview CLI packages for this PR label Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Supabase CLI preview

npx --yes https://pkg.pr.new/supabase/cli/supabase@42ef8e6fac4686aa20ef4c0117eeb3bacb2be16b

Preview package for commit 42ef8e6.

@jgoux jgoux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The native root startup path loses PostgreSQL bundle access when another service or database tool prepares an artifact. The inline finding includes the verified call path and reproduction.

… shared roots

Artifact preparation and state loading reset the artifact cache root, each
artifact directory, and the stack state root to 0700. Once another service
was prepared after PostgreSQL launched as a non-root user, the server could no
longer reach its own bundle, so new sessions failed to load
session_preload_libraries with `could not access file "supautils"`, and a
config reload could not read postgresql.conf. Those roots now stay restricted
to their owner while keeping an existing traverse-only grant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01REDom2C1Z1DUi2df7xGMMv
@jgoux
jgoux self-requested a review September 25, 2026 10:29
@avallete
avallete added this pull request to the merge queue Sep 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 25, 2026
@avallete
avallete added this pull request to the merge queue Sep 25, 2026
Merged via the queue into develop with commit 70b42b8 Sep 25, 2026
49 checks passed
@avallete
avallete deleted the avallete/postgres-root-fallback-938526 branch September 25, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-preview-packages Publish pkg.pr.new preview CLI packages for this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants