Docs / Guides

Data and persistence

Postgres for an app's data or on its own, $DATA_DIR for its files, and what survives redeploys, expiry and claiming.

Keep an app's data in Postgres: add addons: [postgres] to the manifest and every service gets a DATABASE_URL. Files, such as uploads, go in $DATA_DIR, one persistent directory per service. Everything else on disk belongs to a single deploy and is replaced by the next one.

name: notes
addons: [postgres]
services:
  api:
    runtime: python-3.12
    start: uvicorn main:app --host 127.0.0.1 --port $PORT

Postgres beta#

The Postgres add-on is in beta. There is no SLA and no backup beyond the node's own storage, and a beta database may be reset or moved, with notice where we can give it. Keep anything you can't afford to lose somewhere else as well.

This is the way to keep an app's data. The add-on runs on Cellar, our serverless Postgres: each database is a real Postgres, stopped when nothing is connected and started again by the next connection. A stack that lists addons: [postgres] in its manifest gets a Postgres database of its own, and every service gets its URL as DATABASE_URL. All of a stack's services share it, which $DATA_DIR can't do. It lives as long as the stack, through redeploys, expiry and claim, and is dropped when the stack is deleted. Migrate when the process starts, not in build, and in a way the previous version can live with: a failed redeploy rolls the code back but not the data. See Add-ons and its limits.

A database on its own beta#

A stack can be just a database: no code, only a connection string, for an app that runs somewhere else or on your own machine. One command creates it and prints the string:

agentserve db create --name orders

It is a stack whose manifest is only addons: [postgres], linked to the current directory like a deploy, so the rest works as for any stack: it expires unless claimed (the claim link keeps it), agentserve destroy drops it, and its agent or owner can read the connection string again at any time:

agentserve db url                 # postgresql://owner:…@db-orders-x1y2.db.agentserve.sh:5432/db?sslmode=require
agentserve db reset-password      # a new password; the old string stops working

Over the API, create it with a manifest and no source, then read GET /v0/stacks/{id}/database. From an agent, the MCP tool create_database does the same and can write the string to a .env file instead of returning it. Any stack with the add-on has the same connection string, so you can also reach an app's database from your machine, to inspect it or run a migration.

Connections are encrypted (sslmode=require). The certificate isn't one a client can verify yet, so use require rather than verify-full; in node-postgres, pass ssl: {rejectUnauthorized: false} and leave sslmode out of the string. While the stack is expired the database is closed to connections, and claiming it opens it again with its data. Limits are those of the add-on: size and connections.

$DATA_DIR#

The directory is per service, and it is the same directory across every generation of the stack. It is set for the running process and for the build commands. Read it from the environment rather than hard-coding a path:

import os

UPLOADS = os.path.join(os.environ.get("DATA_DIR", "."), "uploads")
os.makedirs(UPLOADS, exist_ok=True)
const path = require("node:path");
const uploads = path.join(process.env.DATA_DIR ?? ".", "uploads");

Falling back to . keeps the code working on your machine, where DATA_DIR isn't set.

Services in the same stack each get their own directory and don't share one.

What survives what#

event$DATA_DIReverything else
Crash and automatic restartKeptKept (same build)
Restart from the dashboardKeptKept (same build)
RedeployKeptReplaced by the new build
Failed redeploy, rolled backKept, including anything the failed version wroteThe previous build comes back
ExpiryKept in the snapshot for 7 daysKept in the snapshot for 7 days
Claim (live or expired)KeptKept; an expired stack restarts from its build
Server restartKeptKept; live stacks are started again
Destroy, or retention running outDeletedDeleted

"Everything else" covers the service's source directory, its virtualenv or node_modules, build output, and any file the process writes next to its code. A redeploy extracts the new source into a fresh directory, and older generations are deleted once the new one is live. A SQLite file written to the working directory is therefore gone after the next deploy, even though it looked fine until then.

Destroying a stack (agentserve destroy, DELETE /v0/stacks/{id}, or deleting the project in the dashboard) deletes its data immediately. An unclaimed stack that expired and isn't claimed within 7 days is deleted the same way.

AgentServe keeps no backups and no history of $DATA_DIR. If the data matters, export it yourself.

SQLite tips#

SQLite in $DATA_DIR works when a single service owns its data, or on a server without the Postgres add-on. A few habits make it hold up:

  • Create the schema at start-up, idempotently. Use CREATE TABLE IF NOT EXISTS or a migration tool that records what it has applied. The process starts against an existing file after every redeploy, restart and restore, and against an empty directory only the first time.
  • Run migrations when the process starts, not in build. Build commands also see $DATA_DIR, but during a redeploy they run while the previous generation is still serving from the same file.
  • Write migrations that the previous version can live with. A failed redeploy rolls the code back but not the data. Adding a column or a table is safe; renaming or dropping one can break the version that comes back.
  • Turn on WAL mode (PRAGMA journal_mode=WAL) if the service reads and writes concurrently, for example from several threads or async workers.
  • Commit explicitly. A crash restarts the process with backoff; uncommitted writes are lost.
  • Keep uploads next to the database. User files go in $DATA_DIR too, for example $DATA_DIR/uploads/.

Static services#

Static services have no process, so nothing writes at runtime. Their files are the build output, rebuilt on every deploy. A frontend that needs to store something should call a backend service that writes to the stack's database.

There is no managed Redis or object storage. To use a database you host elsewhere, pass its connection string in env, and keep in mind that the manifest is uploaded with your source, so anything in env is stored by the server in plain text.

See Limits for the other constraints on a stack.

Stuck? Your agent can read /skill.md, or connect it over MCP.