Skip to content

Databases

When your app uses PostgreSQL, MySQL, MariaDB, MongoDB or SQLite, OpsCrew runs the database next to it on the same server, with a memory limit, a generated password and a data folder that every deploy keeps. It costs nothing beyond the server memory it takes, which design shows with its share of the server’s price (see Components).

Database Version Memory limit (lowest) Smallest server Licence
PostgreSQL 17 512 MB (256 MB) 1 GB PostgreSQL licence
MySQL 8.4, the long-term release 512 MB (384 MB) 1 GB GPL 2.0
MariaDB 12.3, the long-term release 384 MB (256 MB) 1 GB GPL 2.0
MongoDB 8.0 512 MB (384 MB) 1 GB SSPL

OpsCrew picks the database from the client library your app uses (pg, mysql2, mongoose, pymongo …). MariaDB runs only when your app asks for MariaDB itself (for example Laravel’s DB_CONNECTION=mariadb); a MySQL app gets MySQL, since some of its data types do not load in MariaDB. The GPL and the SSPL put no conditions on your app’s own code when you run the database for it; they are about sharing or offering the database software itself.

Each database runs from its official image, pinned by digest. Dependabot proposes newer releases of the same major version in your repository, never a new major one: moving to a new major version can need a data migration, so that choice is yours.

Database Variables Address
PostgreSQL DATABASE_URL postgresql://app_runtime:<password>@db:5432/app
MySQL DATABASE_URL mysql://app:<password>@mysql:3306/app
MariaDB DATABASE_URL mysql://app:<password>@mariadb:3306/app
MongoDB MONGODB_URI, DATABASE_URL mongodb://app:<password>@mongodb:27017/app?authSource=admin

When your app reads its own variable for the address (for example MYSQL_URL), that one gets the address too. OpsCrew generates each password, keeps it on the computer that ran npx opscrew provision (never in your repository) and hands it to the server through your repository’s encrypted deploy secrets. The database listens only on the app’s own network: no port is open to the internet, or to other apps on the server.

PostgreSQL’s address names app_runtime, a login that can only read and write data, and the release command gets the same variables with app_owner (see Two logins). A PostgreSQL made before OpsCrew 0.2.12 shows app here and in the split settings until it is switched, and one older than version 15 until it is upgraded.

Many PHP apps read the database’s host, port, name, user and password one by one instead of an address. When your app reads DB_HOST, it gets them all from its PostgreSQL, MySQL or MariaDB, by the names it reads. OpsCrew derives them and never asks for them:

Setting Names the app reads PostgreSQL MySQL MariaDB
Host DB_HOST db mysql mariadb
Port DB_PORT 5432 3306 3306
Database DB_DATABASE or DB_NAME app app app
User DB_USERNAME or DB_USER app_runtime app app
Password DB_PASSWORD or DB_PASS generated generated generated

For a database on a server of its own, the host is its private address. With two SQL databases, the app gets the settings of the one its DATABASE_URL names, else of the first one, and design names it. The password is kept like the other generated passwords. For a web installer that asks for it once, read it on your computer: see An installer that asks for the database’s password.

Laravel reads these names too, so a Laravel app on MySQL, MariaDB or PostgreSQL gets them the same way. When its .env.example leaves the DB_HOST line commented out, as a new Laravel app’s does, the app gets the database’s address as DB_URL instead, the name Laravel reads it from. Its REDIS_HOST, REDIS_PORT and REDIS_PASSWORD come from the Valkey, Redis or Dragonfly it uses. A value of its .env.example that chose a service, such as DB_CONNECTION=pgsql or QUEUE_CONNECTION=redis, is given with that value, so the app uses the service OpsCrew runs. The PHP driver of each service is in the image (see A plain PHP app).

Your app never logs in to PostgreSQL as its superuser. A bug or an injection in the app can then read and write the app’s data, but it can’t change the schema, create logins, read password hashes or reach the server through the database. Each PostgreSQL 15 or later has three logins:

Login Used by What it may do
app_runtime The web, the workers, cron and a preview’s seed Read, add, change and delete rows (SELECT, INSERT, UPDATE, DELETE) and use sequences. Nothing else.
app_owner The release command, which runs your migrations Owns the database, its schemas and everything your migrations create, so it can change the schema.
app OpsCrew only: the setup, snapshots, restores, moves and rotation The superuser. No container of your app gets its password.

Design writes logins: separate under the database’s options in opscrew/plan.yaml, and the first deploy makes app_owner and app_runtime before the release command runs. The release command reads the owner’s login from its own file, release.db.env, beside app.env, so no other container of your app has it.

  • Nothing at runtime may change the schema. CREATE TABLE, ALTER TABLE, CREATE TEMP TABLE and TRUNCATE from the web, a worker or cron are refused. Put every schema change in a migration, which the release command runs as app_owner.
  • Row-level security applies to your app. app_runtime doesn’t own the tables, so a policy on a table limits the rows it sees, as it never did for the superuser.
  • Other logins see nothing of the app’s data unless granted. OpsCrew takes away the rights every login gets by default on the app’s schemas and on temporary tables. The logins of your devices and of the monitoring keep working.

Snapshots, restores, rollbacks, the restore drill and moves keep the owners and the grants, so app_runtime works after each of them as before.

Every table and sequence app_owner creates is usable by app_runtime at once, with no grant from you: PostgreSQL’s default privileges give it the same rights as on the tables that exist, serial and IDENTITY ids included. A schema app_owner creates later gets USAGE for app_runtime the same way.

Default privileges cover only what app_owner creates. A table made by another login, for example by hand as the superuser, needs its own grant.

Anything beyond reading and writing rows is yours to grant, in a migration, which runs as the owner. Guard the grant, so the same migration also runs on a database on your computer that has no app_runtime:

DO $$
BEGIN
IF EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_runtime') THEN
GRANT TRUNCATE ON TABLE cache TO app_runtime;
GRANT TEMPORARY ON DATABASE app TO app_runtime;
END IF;
END $$;

The first grant lets the app empty the table cache; the second lets it create temporary tables. Grant only what your app uses.

Extensions your app created as the superuser before the switch stay the superuser’s, and their functions keep working for both logins.

Some libraries create their schema or tables when the app starts, such as pg-boss for job queues. As app_runtime, that is refused. Create the schema in a migration instead, and let the runtime login create objects in it:

CREATE SCHEMA IF NOT EXISTS pgboss;
DO $$
BEGIN
IF EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_runtime') THEN
GRANT USAGE, CREATE ON SCHEMA pgboss TO app_runtime;
END IF;
END $$;

Then start pg-boss with createSchema: false, so it doesn’t try to create the schema itself. The tables it makes there are app_runtime’s own. The same works for any library that creates its own schema when the app starts.

Separate logins need PostgreSQL 15 or later; OpsCrew runs 17. A database that pins an older version keeps a single login, app, the superuser, and design and npx opscrew provision say so. Nothing refuses the older version, but adding logins: separate to it by hand is refused. To get separate logins, upgrade the database to 15 or later first (a new major version can need a data migration), then add logins: separate.

A PostgreSQL made before OpsCrew 0.2.12 keeps its single login, and upgrading OpsCrew changes nothing on the server. Each npx opscrew provision then says how to switch. Before you do, make sure nothing in your app changes the schema outside its migrations (see above).

  1. Add logins: separate under the database’s options in opscrew/plan.yaml:

    services:
    - name: db
    kind: postgres
    version: "17"
    options:
    logins: separate
  2. Run npx opscrew provision. It shows the switch as a plan you approve by its hash:

    • a snapshot of the database is taken first, and provision stops if it fails (a database that never ran has no data yet, so provision says so and goes on);
    • the database restarts once, for a few seconds, with two new passwords;
    • the next deploy makes app_owner and app_runtime and gives app_owner the database, its schemas and the app’s tables, in one transaction;
    • from that deploy, the release command logs in as app_owner, and the app, workers and cron as app_runtime.
  3. Deploy. If the setup fails, nothing changes: the deploy stops before the release command, and the running version keeps its old login. When a login called app_owner or app_runtime already exists with other rights, the setup says so: drop or rename it, then deploy again. A database with event triggers is refused: remove them with DROP EVENT TRIGGER first.

There is no going back: once a plan has logins: separate, provision refuses to remove it, since that would give the app the superuser again.

npx opscrew rotate db changes the passwords of all three logins. On a server, none of them restarts the database (in your Portainer, see the credential rotation page). See Rotating your app’s credentials.

Apps that need embeddings (pgvector), geospatial data (PostGIS), or built-in PostgreSQL contrib extensions declare them under options.extensions on the PostgreSQL service in opscrew/plan.yaml. OpsCrew installs each extension as the superuser app before your release command (migrations) runs, so neither app_owner nor app_runtime ever needs superuser rights.

services:
- name: db
kind: postgres
version: "17"
options:
logins: separate
extensions:
- vector
- pg_trgm
Extensions Image OpsCrew runs Architectures
pgcrypto, uuid-ossp, pg_trgm, citext, hstore, unaccent, fuzzystrmatch, btree_gin, btree_gist, ltree, intarray, cube, earthdistance, tablefunc, isn, lo, bloom postgres:17-alpine (no image change) amd64, arm64
vector (plus any of the contrib extensions above) pgvector/pgvector:0.8.7-pg17-trixie (pinned by digest) amd64, arm64
postgis, postgis_raster, postgis_sfcgal (plus any of the contrib extensions above) postgis/postgis:17-3.7-alpine (pinned by digest) amd64 only
  • PostGIS needs an amd64 server: the official postgis/postgis image publishes only linux/amd64 manifests, so design and npx opscrew provision refuse placing a PostGIS database on an arm64 host (your own server, a Portainer host, this computer, or an ARM cloud server type).
  • vector and PostGIS cannot be combined in one database: no single official image ships both, so OpsCrew refuses combining them in the same PostgreSQL service before anything changes. Any unsupported extension name is refused the same way, listing the supported names above.
  • Snapshots, restores, restore drills, and moves run with the database’s extension image and preserve vector indexes, PostGIS geometry data, and spatial_ref_sys under Two logins (keeping app_owner non-superuser).

Extensions are set up when the database is created, before Two logins hands schema ownership to app_owner. OpsCrew records the database’s extension set in its local generated values when npx opscrew provision creates it:

  • If a database already has data, npx opscrew provision refuses adding or removing extensions in opscrew/plan.yaml before anything changes. Keep extensions as it was when the database was created (or create a new database with the extensions you need).
  • If a database has never started yet (has no data), you can still adjust options.extensions before the first deploy.

A PHP app opens a new connection to PostgreSQL on every request, and PostgreSQL starts a new process for each one, which can add more than a tenth of a second to every page that reads the database on a small server. PgBouncer sits between the app and PostgreSQL, keeps a few of PostgreSQL’s connections open and hands them to each request in turn, so pages get faster without a change to your code.

The pooler needs separate logins. Add pooler: pgbouncer next to logins: separate under the database’s options in opscrew/plan.yaml:

services:
- name: db
kind: postgres
version: "17"
options:
logins: separate
pooler: pgbouncer

Then run npx opscrew provision and deploy. Without logins: separate, provision refuses the pooler before it changes anything. For a new PHP app with PostgreSQL 15 or later, design writes pooler: pgbouncer for you; for a PHP app already deployed without it, each npx opscrew provision prints a line on how to add it, and changes nothing because of it.

The deploy then runs a service called <db>-pooler (db-pooler for db) next to the database, with a memory limit of 32 MB and no port open outside the app’s network. Only the app’s web process and cron reach the database through it: in app.env, DATABASE_URL and the split settings name db-pooler and port 6432 instead of db and 5432, with the app_runtime login. For a database on a server of its own, the pooler runs on that server, at its private address. The pooler holds no data, so snapshots, restores and moves work as without it. A worker or service of your app can’t be called <db>-pooler.

The release command, which runs your migrations as app_owner, never goes through the pooler: its release.db.env keeps PostgreSQL’s own address, db and port 5432, by every name app.env gives the database under, DB_HOST and DB_PORT too.

Your workers (queue:work, Horizon and the like) don’t go through it either. A worker keeps its connection open for as long as it runs, so through the pooler it would hold one of the pooler’s connections all that time, and a few workers would leave web requests waiting. The workers read worker.db.env after app.env: PostgreSQL’s own address with the app_runtime login. The deploy writes it next to app.env, readable only by its owner, and a credential rotation and a move rewrite it with app.env.

The pooler gives each connection of your app a PostgreSQL connection of its own for as long as it stays open, which for PHP is one request. When the request ends, the connection goes back to the pooler, which resets it (DISCARD ALL) and hands it to the next request, already warm.

  • Everything works through it: prepared statements, SET, locks, LISTEN and temporary tables all behave as they do on a direct connection, for as long as that connection stays open.
  • Up to 20 at a time: at most 20 of your app’s connections are open at once. A request beyond that waits until one is free, which for PHP’s short requests is a matter of milliseconds.
  • At most 100 waiting or open: past 100 connections to the pooler at once, it refuses new ones.

Apps in Node.js, Python, Ruby, Go and most other languages keep their own pool of connections inside the app, open for as long as the app runs. Through the pooler, each of those would hold one of its connections all that time, so they gain nothing from it, and design doesn’t write it for them.

  • MySQL gets a buffer pool of its memory limit minus about 400 MB for everything else (at least 128 MB), the performance schema off and at most 50 connections, in a settings file next to opscrew/runtime/compose.yaml. A larger memory limit gives it a larger buffer pool.
  • MongoDB gets a cache of half its memory limit (at least 0.25 GB), set explicitly, because MongoDB may not see the container’s limit on its own.
    • A replica set of one member, which Prisma and multi-document transactions need. OpsCrew turns it on for a Prisma app; for transactions without Prisma, set options: { replicaSet: true } on the service in opscrew/plan.yaml. The deploy sets it up the first time, and the address then names it.
    • On x86 servers, MongoDB needs a CPU with AVX. Some older servers you may already have lack it, and MongoDB would crash at start, so on a server you own npx opscrew provision checks the CPU first and stops with a plain message when AVX is missing.
  • PostgreSQL and MariaDB run with their image’s defaults.

On a server, each database keeps its files in /srv/data/<app>/<name>, for example /srv/data/shop/db for PostgreSQL. The folder belongs to your app alone, so several apps on one server never share one. In your Portainer, the data is in the named volumes of the app’s stack.

Deploys never recreate a database container unless its own settings or image changed. npx opscrew destroy keeps the data unless you add --delete-data and type the app’s name.

When your app already uses a hosted database (a mongodb+srv:// address from MongoDB Atlas, PlanetScale, Neon …), the interview asks whether to keep it or let OpsCrew run one on the server. If you keep it, OpsCrew runs nothing for it: set its address in your app’s production environment.

On Hetzner Cloud and DigitalOcean, the provider’s server backups (on by default in prod) cover the database folders. They copy a running server, so they are crash-consistent only: the database recovers from them as after a power cut, and the last seconds of writes can be missing. For a copy you can restore anywhere, dump the database from the app’s folder on the server, for example:

Terminal window
cd /srv/apps/shop
docker compose exec -T db sh -c 'pg_dump -U "$POSTGRES_USER" "$POSTGRES_DB"' > backup.sql
docker compose exec -T mysql sh -c 'mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" app' > backup.sql
docker compose exec -T mongodb sh -c 'mongodump --archive -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin' > backup.archive

On every target, npx opscrew snapshot copies every database, SQLite included, and npx opscrew restore <id> puts it back. Scheduled snapshots with an encrypted copy off the server are the backup on a server you already have and in your Portainer, where no provider backs up the data: see Snapshots and restores.

On every environment but previews, prod included, each deploy takes a snapshot of the app’s data before its release command, so that a rollback can restore it: the databases, SQLite, the other services with data, and a database on another server. The snapshots stay in .snapshots in the app’s folder, for example /srv/apps/shop/.snapshots, readable only by the deploy user:

  • ClickHouse and Neo4j are copied too, except in your Portainer, each stopped for a moment at every deploy while it is copied; the services named in deploy.snapshotStop replace them (snapshotStop: [] keeps them running, out of the snapshot; see Snapshots before each release);
  • deploy.snapshots: false in an environment’s plan turns them off there;
  • deploy.keepSnapshots (1 to 10, default 3) says how many versions’ snapshots stay. Each takes about as much disk as the database’s data; with too little free space a deploy skips its snapshot with a warning.

Run npx opscrew provision after changing any of them. They live on the same server as the database, so keep a copy elsewhere too: the server backups, the dumps above, or the encrypted copy off the server.

A SQLite database is a file inside your app, so OpsCrew does not run a database for it. Instead:

  • The database’s folder is a persistent volume. storage.sqlite.dir in opscrew/plan.yaml names the folder, relative to your app (for example storage for Rails 8). It is mounted from the Docker volume <app>_sqlite, which every deploy keeps, together with SQLite’s -wal and -shm files.
  • The folder must hold only the database, or the volume would hide your other files from the app. Laravel’s database/database.sqlite (next to the migrations) moves to storage/database, and a Prisma database inside prisma/ (next to the schema) moves to data, with the app’s variable (DB_DATABASE, DATABASE_URL) pointed at the new place. For a database anywhere else next to your code, such as Django’s db.sqlite3 beside manage.py, design stops and asks you to move it into a folder of its own, for example data/app.sqlite3.
  • Where the folder is in the image: under the app’s folder there: /workspace with Buildpacks, /app with Railpack and with OpsCrew’s Dockerfile (/app/<folder> for a Node.js app in a subfolder of the repository), and the last WORKDIR of your own Dockerfile.
  • Deploys stop the old version before the new one starts, so two copies never write to the database at once (deploy.strategy: recreate). Requests wait meanwhile, so visitors see a short pause, not an error (see Small servers restart the app instead).

Docker gives a new volume the owner of the folder in the image. OpsCrew’s Dockerfile, and the step OpsCrew adds to Buildpacks and Railpack images, make that folder the app’s own user’s, so the app can write its database. With your own Dockerfile, that is up to the Dockerfile: a folder it copies as root (COPY without --chown) stays owned by root, and an app that runs as another user cannot write to it. Give the folder to the app’s user before the USER line, for example:

RUN mkdir -p /app/storage && chown app:app /app/storage
USER app

The volume keeps the owner it got when it was created. When the image later runs as another user, the app cannot write its database until the volume’s owner changes too:

  • When you change the builder (app.build.builder in opscrew/plan.yaml), the image’s user changes with it: Buildpacks runs apps as 1002:1001, Railpack as 10001:10001, OpsCrew’s Dockerfile as 1000:1000 (Node.js) or 10001:10001 (Python). npx opscrew design warns you and prints the command for your app.
  • When you change USER in your own Dockerfile, OpsCrew does not notice. Change the owner yourself before you deploy the new image.

When you change the builder, npx opscrew design prints the exact command for your app, with the edge’s Caddy image as OpsCrew pins it (the image is already on the server and has chown). It has this form, with the new user’s numbers:

Terminal window
docker run --rm --user 0 --entrypoint chown -v <app>_sqlite:/d <the edge's Caddy image> -R <uid>:<gid> /d

After a change of USER in your own Dockerfile, use the same form with that user’s numbers and the image named in the caddy service of /srv/edge/compose.yaml on the server. In your Portainer, design names a one-off container to run instead: the pinned Caddy image, user 0, entrypoint chown, command -R <uid>:<gid> /d, with the stack’s volume <stack>_sqlite mounted at /d.