MongoDB and PostgreSQL
Run Spica on MongoDB or PostgreSQL, set each one up correctly, and know the differences your applications will see.
Spica runs on either MongoDB or PostgreSQL. You choose the engine when you create an instance with --database-type, and it stays the same for the instance's lifetime. Both engines support buckets, functions, passport, storage, realtime and the rest of the platform.
MongoDB
--database-type=mongodb \
--database-uri="mongodb://mongo-0,mongo-1,mongo-2/?replicaSet=rs0" \
--database-name=spica \
--database-replica-set=rs0- MongoDB must run as a replica set, even with a single node. Spica uses change streams for realtime, triggers and webhooks.
- Credentials can be passed with
MONGODB_USERNAMEandMONGODB_PASSWORD. - Tune
--database-pool-size,--database-read-preferenceand--database-change-stream-await-timefor your load.
PostgreSQL
--database-type=postgresql \
--database-uri="postgres://postgres:5432/spica"- Use the
spicaengine/postgresimage, or a PostgreSQL server with thepg_cronextension. Spica uses it to expire logs, statuses, tokens and other short-lived records. - Credentials can be passed with
POSTGRES_USERNAMEandPOSTGRES_PASSWORD. --database-nameis ignored; the database is part of the URI.
Differences your applications see
| Topic | MongoDB | PostgreSQL |
|---|---|---|
| Resource ids | _id, an ObjectId string | id, a UUIDv7 string |
| Document keys | _id on every document | The bucket's primary property, which must be uuid, integer, bigint or string and can be generated |
| Extra property types | uuid, integer, bigint (returned as a string) | |
| Bucket filters | Expressions and JSON (MongoDB query) filters | Expressions only |
| Translated properties | Any property type | Scalar types |
| Relations inside arrays | Supported | Not supported |
| Index options | Any MongoDB index option | unique, sparse, background. 2dsphere indexes have one property and aren't unique. |
Document limit with remove | Supported | Not with a string primary key |
| GraphQL | Supported | Not available |
@spica-devkit/database | Supported | Not available |
@spica-devkit/testing | Supported | MongoDB only |
A PostgreSQL bucket and a document look like this:
{"id": "01a2235d-7c41-789b-b7f5-fb7beb95c31e", "title": "Posts", "primary": "id", "properties": {"id": {"type": "bigint", "options": {"generated": true}}}}{"id": "1", "slug": "hello-spica", "title": "Hello Spica", "views": 42, "author": "01a2235d-7c6a-7fab-b705-48e72471385d"}Writing portable code
If your code must run on both engines:
- Use expression filters everywhere.
- Read ids through a helper that checks
_idand thenid. - Avoid engine-only features from the table above.
Choosing an engine
Choose the database your team already operates well. MongoDB suits flexible, nested documents and makes JSON filters and GraphQL available. PostgreSQL suits teams that standardize on relational databases, want typed primary keys, or run managed PostgreSQL services.
Running the database
| Setup | MongoDB | PostgreSQL |
|---|---|---|
| CLI | spica project start <name> | spica project start <name> --database-type postgresql |
| Helm | database.type: mongodb | database.type: postgresql |
| Managed | MongoDB Atlas or any replica set | Any PostgreSQL with pg_cron |
Back up the database regularly, together with the volume or bucket that holds files and function sources.