Services

A database for each environment, which an app asks for in its manifest.

On this page

An app can ask for a database or an object store in its manifest, and Moorage runs it and connects the app to it. Each environment gets its own, so that a branch's preview has a database of its own to break.

[services.db]
type = "postgres"

That is all it takes. On the next deployment, Moorage starts PostgreSQL 18 for that environment, waits for it to be ready, and then gives your app DATABASE_URL. A release command can migrate, as the service is ready before it runs.

This is the one thing in Moorage, apart from apps in containers, that needs a container engine. Podman or Docker has to be there for an app that asks for a service, and nothing else cares. A deployment that asks for one with no engine answering says so before it builds anything.

In the manifest#

KeyMeansDefault
typepostgres or s3Required
versionFor postgres, 15 to 1918. It is recorded when the service is made, and does not move by itself
imageFor postgres, another image based on the official one, such as pgvector/pgvector:pg18, which is the way to get extensions. It needs a tag other than latestThe official image
envAnother name for the variable, or for s3 the start of each, needed for a second of one typeDATABASE_URL, or S3_ENDPOINT and its fellows
memoryA limit, such as 512MiBNone
previewsown, or none for previews to have noneown
seedproduction, for a new preview to start with a copy. See what a preview starts withEmpty
sanitizeWith seed, a script in the app that removes what should not be copied, or none to say that nothing should

Two of a type need different variables:

[services.db]
type = "postgres"

[services.analytics]
type = "postgres"
version = "16"
env = "ANALYTICS_DATABASE_URL"

An object store#

[services.files]
type = "s3"

It gets one bucket, and one key that can do anything in it:

VariableIs
S3_ENDPOINT and AWS_ENDPOINT_URLIts address, such as http://127.0.0.1:27014
S3_BUCKETapp
S3_REGION and AWS_REGIONus-east-1, which means nothing here and which clients want
S3_ACCESS_KEY_ID and AWS_ACCESS_KEY_IDIts key
S3_SECRET_ACCESS_KEY and AWS_SECRET_ACCESS_KEYIts secret key, kept out of what the app prints

With env = "UPLOADS", they are UPLOADS_ENDPOINT and so on, and the AWS_ names are left for another. Clients need path-style addresses, as there is no name for a bucket to go in front of: force_path_style or addressing_style = "path", depending on the client.

It is Versity Gateway, which keeps a bucket as a directory and an object as a file. Everyday S3 works: uploading in parts, presigned URLs, CORS and bucket policies. What does not: versioning, lifecycle rules, object ACLs, and keys a and a/b at once, as a file cannot also be a directory. An app that needs those needs real S3, which it can have by setting these variables on the environment itself, as below.

A presigned URL is signed for its host, so that a browser elsewhere cannot use one for 127.0.0.1. For now, this is for what an app reads and writes for itself, and it has no public address.

Which address an app gets#

From weakest to strongest:

  1. Variables for all environments, and what a preview inherits from production
  2. The address of this environment's own service
  3. A variable set on this environment itself

So production can use a real database by setting its own DATABASE_URL, in which case Moorage starts nothing for it and needs no engine for it, while every preview still gets its own. A preview never ends up on production's database by inheritance: with previews = "none", what it would inherit of that name is left out.

The address is given as an instance starts, as a secret is, along with its password alone as DATABASE_URL_PASSWORD. Neither is in a release or on a command line, and both are kept out of what the app prints. An app that runs natively gets 127.0.0.1 and a port that stays the same for the life of the service. One in a container reaches it by name, on a network for its environment.

What happens to it#

WhenWhat happens
You deployIt is made if it is not there, and started if it has stopped. If it does not become ready, the deployment fails, and the previous release goes on serving
It stops by itself, or the machine restartsMoorage starts it again
The server stops or is upgradedIt goes on running, like your apps
moor server stop --all or moor server uninstallIt stops, after your apps, and keeps what it holds. The server starts it again when it next starts. Uninstalling says how to take it away
The environment goes to sleepIt stops, and keeps what it holds, unless a process with sleep = false or a command is still using it. Waking starts it first, and a command or a cron job starts it for as long as it runs
The manifest stops naming itIt stays, with what it holds, marked as unused. A release that does not name it does not get its address
The environment is deletedIt goes, with what it holds
The engine stops answeringWhat is running is left alone. Deploying, restarting, putting to sleep or deleting says why it cannot, and leaves what is running as it is. server.engine_down says so, to the rules that ask

A manifest that asks for another major version than a service holds is refused, as that takes moving its data. PostgreSQL itself also refuses to start on a newer version's files, so this is not all that stands between a mistake and lost data.

What a preview starts with#

Empty, unless the manifest says otherwise, with [release] on_create to put something in it, which runs once. Copying production into previews is how customer data leaks.

To copy it anyway, it takes saying so, and saying what removes what should not be copied:

[environments.preview]
access = "login"

[services.db]
type = "postgres"
seed = "production"
sanitize = "scripts/sanitize.sql"

A new preview then starts with production's latest dump, which a backup makes. The copy and the script happen where nothing can connect, and if either fails, or the server stops halfway, nothing is left of the copy, and the deployment fails. The script is in the app, and a link in its place that leads out of the app is refused. sanitize = "none" says that nothing needs removing.

A preview starts empty all the same, and its deployment's log says why, if anyone can reach it, if it is the preview of a pull request that comes from a fork, or that nothing says does not, or if a coding agent's token made it. An existing preview is not seeded again: destroy it, or remove its service. A later commit that would let anyone reach a preview holding such a copy fails to deploy, and says so, and moor access refuses to open it. If production has moved to a newer version and the manifest names none, the copy runs that version too. Only the role app gets a new password: any others that the dump holds keep what production gave them, which is for sanitize to deal with.

Backups#

A backup holds a dump of each service of production and of the named environments, made just before: pg_dumpall, or a tar of the object store. One that is asleep is started for that, where no app can reach it, and one that has lost its volume is left with the dump that it has. A dump that fails or does not get to its end is reported, the last good one stays, and the rest of the backup goes on. Volumes are not in a backup, and previews are not dumped. Dumps go in .services in the app's directory of data, where apps that run as users of their own cannot reach them. Without that, an app's code can read them, as it can read anything else of the server's.

After a restore to a new machine, a service comes up on a fresh volume and loads its dump by itself, before anything can connect to it. So does one whose volume has gone for any other reason, and if that does not get to the end, it starts again from nothing on the next try. What was written after the last dump is gone.

On the machine that it ran on, a service still has its volume, and so keeps what it holds, which may be newer than the backup. To go back to the dump:

moor services restore db --yes

PostgreSQL's dump goes into a new volume first, so that one that will not load changes nothing. An object store's dump is checked, and replaces what it holds. What PostgreSQL held stays until you say what to do with it, as it does after an upgrade.

If one of an object store's two volumes has gone, it does not start, and says so: the other may be all that there is of what it held, so that going back to its dump is for you to ask for.

Updates and upgrades#

Nothing changes an image by itself: which one it runs is recorded when a service is made.

moor services update db

That takes the newest image of its version, such as 18.7 where it ran 18.6, and restarts it. One that is asleep stays asleep, and runs it when it wakes.

A new major version takes moving its data, as an image holds only its own version:

moor services upgrade db --to 19

The app stops for that, as what it wrote after the dump would be lost, and commands and cron jobs are stopped and refused until it is over. Moorage dumps the service with the newer version's tools, restores into a new volume, checks that the database app holds as many rows and tables and that the same databases are there, and switches. Anything that would refuse it does so before anything stops. Anything going wrong after that leaves the old one as it was, and the app starts again however it goes. Say the new version in the manifest too, if it names one. A service with an image of its own has to say which holds the new version, with --image. The old volume stays until you say what to do with it. To let go of it:

moor services upgrade db --finish --yes

Or, if you are not happy, to go back to it, losing what the service has held since:

moor services upgrade db --back --yes

After going back from an upgrade, the manifest has to say the older version again, if it names one, and a dump made since by the newer version cannot be loaded, so that it takes the next backup to have one that can.

Going back starts what it held before first, and if that will not start, it goes on as it was. moor services says which of them still keep an old volume. A server that stops halfway through an upgrade or a restore comes back on whichever volume the record names, which is whole, and can leave another behind. moor server doctor says so, and --finish lets go of that too, along with any old volume.

Seeing and removing them#

moor services --app shop
moor logs --service db --env staging
moor services remove db --env my-branch --yes

moor services logs db is the same. Its engine keeps what it says, so that it has only --tail, and what it says goes when it stops.

They are on the Services tab of an app in the console too, with what each has said and what an app's services add up to across its environments, as each preview has its own. What one uses is in metrics as service.db, with an app's instances. moor server doctor says if one that should be running is not. Removing one takes what it holds, for good. If the manifest still names it, the next deployment makes a new one, which starts empty.

How far to trust this#

In production, this is one container with no replica. It is a convenience, and not database hosting. For a database that matters, set DATABASE_URL on production to one that is looked after, and let previews use their own.

Apps on a machine are kept apart from a service by its password, and not by the network. Each has a random password of its own, but a port on the loopback address can be reached by anything on that machine. See how apps are kept apart.

On macOS and Windows, the data lives inside the engine's machine, so that podman machine rm takes it with it.

Suggest a change to this page