Skip to content
KendorDocs

How to add a PostgreSQL, MySQL, MongoDB or Redis database to a coding challenge

For challenge authors ·

List PostgreSQL 18, MySQL 8.4, MongoDB 8.0 or Valkey 9 under databases in kendor.yaml. They run inside the sandbox, and code connects with DATABASE_URL, MONGODB_URI or REDIS_URL.

To give a challenge a database, list it under databases in kendor.yaml. Kendor runs PostgreSQL 18, MySQL 8.4, MongoDB 8.0 and Valkey 9 (Redis compatible) inside the candidate's sandbox on 127.0.0.1, starts them before any install command, and sets connection variables such as DATABASE_URL, MONGODB_URI and REDIS_URL for every process, so the code connects without any setup from the candidate.

Declare a database

kendor.yaml
version: 1
runtime:
  type: node
  version: "22"
databases:
  - type: postgres
    version: "18"
    init: db/seed.sql
  - type: valkey
    version: "9"
services:
  app:
    install:
      commands:
        - npm ci
    dev:
      command: node --watch src/server.js
      port: 3000
    test:
      command: node --test
Key Required Description
type Yes postgres, mysql, mongodb or valkey. Each at most once, and only one of postgres and mysql, since both set DATABASE_URL.
version Yes "18" for PostgreSQL, "8.4" for MySQL, "8.0" for MongoDB, "9" for Valkey, as a quoted string.
name No PostgreSQL, MySQL and MongoDB. The database to create. Default app. Lowercase letters, digits, underscores and hyphens, starting with a letter or underscore, and not a name the engine reserves (postgres, template0, template1, admin, local, config, mysql, information_schema, performance_schema, sys).
user No PostgreSQL, MySQL and MongoDB. The user the app connects as. Default kendor.
password No PostgreSQL, MySQL and MongoDB. That user's password. Default none, except MongoDB, where it is kendor.
init No PostgreSQL, MySQL and MongoDB. A seed file, or a folder of them, relative to the workspace root: .sql for PostgreSQL and MySQL, .js for MongoDB.

You can also add databases in the Runtime configuration dialog under Databases, with an optional Seed file. The Express + PostgreSQL + Valkey project for Node.js is a complete working example.

Seed data with init

init runs once, right after the database is created, as the app's own user:

  • A file runs on its own. A folder runs every seed file in it in name order, so prefix files with numbers: 01_schema.sql, 02_data.sql.
  • PostgreSQL runs everything in one transaction. If any statement fails, nothing is applied.
  • MySQL runs each .sql file in turn and stops at the first error. MySQL can't roll back schema changes, so a failed seed may leave some tables behind.
  • MongoDB runs .js files with mongosh, connected to the app's database. There is no transaction around them, so a failed seed may leave some writes behind.
  • If the seed fails while the sandbox is running, the database keeps running and the error shows in its process. Fix the file, then restart the Database process from the Processes view to run it again.

Migrations that your framework runs (prisma migrate deploy, php artisan migrate) go in the service's migrate commands instead. They run every time the service starts, after its install and before its dev command, so new migration files apply on a restart. When you import a repository, Kendor fills them in from the migration tool it already uses.

Connection details

Every server listens on 127.0.0.1 only. PostgreSQL and MySQL connect as the user kendor with no password. MongoDB has no passwordless login, so its user kendor has the password kendor. The app's user can manage its own database but isn't a superuser.

PostgreSQL

Variable Value
DATABASE_URL postgresql://kendor@127.0.0.1:5432/app (with your name in place of app)
PGHOST, PGPORT, PGUSER, PGDATABASE 127.0.0.1, 5432, kendor, the database name
DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD The same values, for Laravel. DB_PASSWORD is empty.
SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, SPRING_DATASOURCE_PASSWORD jdbc:postgresql://127.0.0.1:5432/app, kendor and empty, for Spring Boot.

When you set a password, PGPASSWORD is set too.

Because these are real environment variables, they take precedence over a Laravel .env file or a Spring application.properties.

MySQL

Variable Value
DATABASE_URL mysql://kendor@127.0.0.1:3306/app (with your name in place of app)
MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE 127.0.0.1, 3306, kendor, empty, the database name
DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD mysql and the same values, for Laravel.
SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, SPRING_DATASOURCE_PASSWORD jdbc:mysql://127.0.0.1:3306/app, kendor and empty, for Spring Boot.

When you set a password, MYSQL_PWD is set too. The server also accepts mysql_native_password logins, for older drivers that can't use MySQL's newer default.

MongoDB

Variable Value
MONGODB_URI, MONGODB_URL, MONGO_URI, MONGO_URL mongodb://kendor:kendor@127.0.0.1:27017/app?directConnection=true (with your name in place of app)
SPRING_DATA_MONGODB_URI The same, for Spring Boot.
DATABASE_URL The same, for Prisma. Not set when PostgreSQL or MySQL is also declared; the SQL database keeps it.

MongoDB runs as a one-node replica set, so transactions and change streams work. The user exists in the app's database and in admin, so a URL with authSource=admin works too.

Valkey

Variable Value
REDIS_URL, VALKEY_URL redis://127.0.0.1:6379
REDIS_HOST, REDIS_PORT 127.0.0.1, 6379
SPRING_DATA_REDIS_HOST, SPRING_DATA_REDIS_PORT The same, for Spring Boot.

Valkey speaks the Redis protocol, so any Redis client works.

If your code reads a different variable name, map it in the service's dev.env.vars with databases.<type>.url, such as databases.postgres.url or databases.mongodb.url:

kendor.yaml
version: 1
runtime:
  type: python
  version: "3.12"
databases:
  - type: postgres
    version: "18"
    name: inventory
services:
  app:
    install:
      commands:
        - pip install --user -r requirements.txt
    dev:
      command: uvicorn main:app --host $HOST --port $PORT --reload
      port: 8000
      env:
        vars:
          SQLALCHEMY_DATABASE_URI: databases.postgres.url

Candidates see the same details in the editor: the Processes view has a Databases section with each connection URL, every variable set for it and a command to open a client in the terminal:

Database Terminal command
PostgreSQL psql
MySQL mysql -h 127.0.0.1 -u $MYSQL_USER $MYSQL_DATABASE
MongoDB mongosh $MONGODB_URI
Valkey redis-cli

Connect from code

Read the URL from the environment. Add the client library to your project's dependencies (pg and redis for Node.js, psycopg and redis for Python) so the install command fetches it.

src/db.js
import pg from 'pg';
import { createClient } from 'redis';

export const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });

export const cache = createClient({ url: process.env.REDIS_URL });
await cache.connect();

const { rows } = await pool.query('SELECT id, title FROM todos ORDER BY id');
await cache.set('todos', JSON.stringify(rows));

What happens to the data

During a session, data persists. Rows are stored in the sandbox's own cache folder, so they survive a restart of the database process or of the whole sandbox, and the init seed doesn't run a second time.

The Tests tab runs against that same live database. Write tests that create the rows they need and clean up after themselves, or wrap each test in a transaction, so a candidate's experiments in the terminal can't change the result.

Limits

Limit
PostgreSQL connections 20
PostgreSQL temporary files 1 GB per connection
MySQL connections 20
MongoDB cache 256 MB
Valkey memory 64 MB
Data on disk 1 GB; past that the database servers stop, and the Database process says how to start over with an empty database

The database ports, 5432, 3306, 27017 and 6379, are never exposed through the preview, even if a server listens on them.

MySQL can't read or write files on the sandbox's disk (LOAD DATA INFILE, SELECT ... INTO OUTFILE), and MongoDB has server-side JavaScript turned off ($where, $function, mapReduce).

Was this article helpful?