Databases

Databases

A database is a managed MariaDB database that belongs to your account, not to one app. You create it under Storage → Databases (/account/databases), give it users, and connect it to one or more of your apps. When you connect an app, ngris gives it the connection details as environment variables (DB_HOST … DATABASE_URL), so you never copy a password into your app.

  • MariaDB only. It works with MySQL drivers and tools. Postgres, Redis and other engines aren't available. If you need one, run it elsewhere and set its URL as a secret environment variable.
  • Its own users. Each database has up to 10 users, each read-write or read-only. A password is shown once, when the user is created or its password is reset.
  • Its own backups. Back a database up on demand or on a schedule, download a backup, or restore one, from its Backups page — see Backups.
  • Only apps on ngris connect. Managed, full-stack and function apps on ngris can connect to a database. Nothing outside ngris can — see Who can connect.
  • Team and Enterprise plans. How many databases depends on the plan — see Plans and limits.
Per-app databases. A backend app can still add a database from its own Database page. That creates an ordinary database under Storage → Databases, named after the app and connected to it, and it stays when you delete the app — see Per-app databases.

Create a database

Open Storage → Databases and click New database (or Create → Database in the top bar). The wizard has three steps:

  1. Name. A label only you see: 1–64 characters, no control characters, unique in the account. You can rename it later. Inside MariaDB the database gets its own name: app_, your name in lowercase letters, digits and _, then _ and 8 generated characters (for example app_orders_db_k3x9p2ab). That name never changes.
  2. Engine. MariaDB.
  3. Review. Check the details and click Create database.

ngris creates the database with a first user named app (read-write). The database's page opens and shows that user's login and password once: copy the password then, because a reload doesn't show it again. Apps you connect as app get the password automatically, so you only need it for your own records. If you lose it, reset the password.

The status is Creating (provisioning) until the database is created, then Ready (ready). The page updates by itself. If creation fails (failed), nothing is left half-made: delete the database in Settings and create a new one.

API

curl -X POST "https://api.ngris.com/v1/databases" \
  -H "X-API-KEY: <your_api_key>" \
  -H "Content-Type: application/json" \
  -d '{"name":"orders-db"}'

The 201 response carries the database, its first user and that user's password, which the API never returns again. Pass initial_user_name to name the first user something other than app. See Create Database and the rest of the Databases API.

Users

Apps connect to a database as one of its users. Manage them on the database's Users page. A database can have up to 10 users.

  • New user. Give it a name (1–32 characters, not starting with _, unique on this database) and pick its access. ngris generates the login, like u_k3x9p2_reporting, and a 48-character password, shown once in the dialog.
  • Reset password. Gives the user a new password, shown once. Apps connected as the user restart to pick it up; until they have restarted, their new database connections fail. (In the API this is rotate.)
  • Delete user. Refused while any app connects as the user: disconnect the app, or connect it as another user, first.

Passwords are never shown again after the dialog closes. Apps connected as a user always get its current password automatically.

Access

Access Privileges on its database Use it for
Read-write (default)SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, CREATE VIEW, SHOW VIEWApps that change data and run their own migrations.
Read-onlySELECT, SHOW VIEWReporting and analytics apps.

A user's privileges cover its own database only. Several apps can connect as the same user, but they then share that user's connection and query-rate caps. Give each app its own user, so you can reset or remove one without touching the others.

Connect an app

Open the app's Environment page. The Connected databases card lists the databases the app uses; click Connect database and fill in:

  • Database — one of your ready databases. Create a new database opens the wizard and brings you back here when it's ready.
  • Connects as — a ready user of that database.
  • Prefix (optional) — leave it empty for DB_HOST, DB_PORT, …; add one to connect more than one database to the app.
  • Use in — Both (production and preview, the default), Production only or Preview only. Preview covers branch previews only; a deploy waiting to be promoted gets the Production variables.

The dialog previews the variables before you save. You can also start from the database: its Connected apps page has Connect an app, which opens that app's Environment page with the database chosen.

The variables an app gets

Variable Value
{PREFIX}DB_HOSTThe database server's internal address (shown on the database's Connection page).
{PREFIX}DB_PORTThe port, 3306.
{PREFIX}DB_NAMEThe database's name inside MariaDB (app_…).
{PREFIX}DB_USERThe user's login (u_…).
{PREFIX}DB_PASSWORDThe user's password. Secret.
{PREFIX}DATABASE_URLmysql://<user>:<password>@<host>:<port>/<database>. Secret.

With no prefix the names are exactly DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD and DATABASE_URL. A prefix is uppercased and gets a trailing _ if it lacks one, so orders becomes ORDERS_ and the app gets ORDERS_DB_HOST … ORDERS_DATABASE_URL. A prefix starts with a letter, has up to 30 more letters, digits or _, and ends in _.

What happens when you connect

  • The app restarts. If the app is running, saving restarts it so it picks up the variables. If it isn't running yet, it gets them on its next deploy.
  • The password stays out of your variables. ngris builds the variables from the connection when it starts the app; the password is never stored among the app's environment variables.
  • Locked rows. The Environment page lists the variables with a lock and a database label; DB_PASSWORD and DATABASE_URL are masked. You can't edit or delete them there: use Open database or Disconnect. A row marked pending reaches the app once the database and its user are ready; ngris then restarts the app again.
  • The names are reserved. Connecting is refused if the app already uses one of the names, as a variable you set or through another connection, in an overlapping environment (Both overlaps everything). The error lists the names; remove them or choose a prefix. While the connection exists, you can't set a variable with one of its names. (Variable names starting with DB_, DDL_ or WORDPRESS_DB_, and DATABASE_URL, are reserved by ngris on backend apps in any case.)

Disconnect (on the Connected databases card, a locked variable row, or the database's Connected apps page) removes the variables from the app and restarts it if it's running. The database and its users stay.

API

curl -X POST "https://api.ngris.com/v1/applications/{uuid}/database-connections" \
  -H "X-API-KEY: <your_api_key>" \
  -H "Content-Type: application/json" \
  -d '{"database_uuid":"<database uuid>","user_uuid":"<user uuid>","env_prefix":"orders","environment":"all"}'

See Database Connections. The API also accepts a database or user that is still being created: the response then says env_pending: true, and the variables arrive once both are ready.

Use the connection in your code

Read the variables from the environment when your app starts; never hard-code the password. The examples use the names without a prefix; with a prefix such as ORDERS_, read ORDERS_DATABASE_URL, ORDERS_DB_HOST and so on.

Node (mysql2 / Prisma)

Use DATABASE_URL directly — both accept the mysql:// URL as-is.

// mysql2
import mysql from "mysql2/promise";
const db = await mysql.createConnection(process.env.DATABASE_URL);

// Prisma (schema.prisma)
datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

Python (SQLAlchemy)

SQLAlchemy needs a driver in the URL scheme (e.g. mysql+pymysql://). Either swap the scheme on DATABASE_URL, or build the URL from the parts so the scheme is explicit:

import os
from sqlalchemy import create_engine

url = (
    f"mysql+pymysql://{os.environ['DB_USER']}:{os.environ['DB_PASSWORD']}"
    f"@{os.environ['DB_HOST']}:{os.environ['DB_PORT']}/{os.environ['DB_NAME']}"
)
engine = create_engine(url)

Go (go-sql-driver/mysql)

⚠ The Go driver does NOT accept the mysql:// URL. go-sql-driver/mysql wants a DSN in the form user:pass@tcp(host:port)/dbname, not a URL. Passing DATABASE_URL to sql.Open is a common, silent failure — build the DSN from the DB_* variables instead.
import (
    "database/sql"
    "fmt"
    "os"

    _ "github.com/go-sql-driver/mysql"
)

dsn := fmt.Sprintf(
    "%s:%s@tcp(%s:%s)/%s?parseTime=true&charset=utf8mb4",
    os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"),
    os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"),
)
db, err := sql.Open("mysql", dsn)

The parseTime=true&charset=utf8mb4 query keeps DATETIME/TIMESTAMP columns scanning into time.Time and gives you full Unicode (including emoji).

Migrations and seed data

A new database is empty; your app owns the schema. Run migrations when the app starts (for example Prisma migrate deploy, an ORM auto-migrate, or a golang-migrate step in your entrypoint), before the server accepts traffic. The build can't reach the database, so migrations can't run during the build. A read-write user has the privileges migrations need (CREATE, ALTER, INDEX, DROP, views).

Who can connect

Only your apps that run on ngris can reach a database: managed (backend), full-stack and function apps. The database server has an internal address only. There is no outside access:

  • not from your computer, CI or another cloud;
  • not from a service behind an agent tunnel;
  • not from edge functions;
  • not from the build (run migrations when the app starts).

Static sites can't use a database: they run in the visitor's browser, with no server to connect from. WordPress sites use their own built-in database; other databases can't be connected to them.

Plans and limits

Databases are included on the Team and Enterprise plans; the Developer (free) plan doesn't include them. Pay-as-you-go plans don't include databases either.

How many databases an account can have, and the storage per database, depend on the plan. The Databases page shows how many you use and your plan's limit. The API returns them as quota.limit and storage_limit_bytes in GET /v1/databases, and as plan_limits.max_managed_databases in GET /v1/account/plan. At the limit, New database is disabled and the API answers 403 “You've reached your plan's limit of N managed databases — delete one or upgrade your plan”. A database that failed or is being deleted doesn't count.

  • Users: up to 10 per database, on every plan that includes databases.
  • Storage: the plan sets the storage per database, and ngris measures each database's size regularly and enforces it (below). The API returns the size as size_bytes with size_measured_at in GET /v1/databases/{uuid}; the database's Overview page still shows Measured soon for now.
  • Tables: only InnoDB tables can be created (CREATE TABLE … ENGINE=InnoDB, the default). A table with another engine is refused.

When a database reaches its storage limit

  • 80 % and 100 %. The account owner gets an email when the database reaches 80 % of its limit, and another at 100 %.
  • Writes are blocked 24 hours after the database reached 100 %, if it is still at or over the limit, or right away once it goes past 110 %. Inserting, updating and creating or altering tables then fail with a permission error, and open connections are closed so the app reconnects.
  • What still works: reads (SELECT), deletes (DELETE, TRUNCATE) and dropping tables (DROP TABLE), so you or your app can free space. Backups and restores keep working too.
  • Writes come back on their own once the database is under 95 % of its limit, or when the limit no longer applies (for example after an upgrade). The 100 % warning and its 24 hours then start over.

The database's writes_blocked_at in the API says when writes were blocked; it is absent while writes work. To get room, delete data you don't need, or move to a plan with more storage per database.

Paused databases

When your account has more databases than its plan allows (for example after a downgrade), the extra ones are paused. A paused database is also closed: apps can't connect to it. An app connected to it for Both or Production only is paused with it; a Preview only connection never pauses an app. A paused database's settings are read-only: you can't rename it, add users, reset passwords, connect apps, restore a backup into it or turn its automatic backups on (the API answers 409 with the code quota_paused). You can still delete the database or its users, disconnect apps, download and delete backups, and turn automatic backups off. Because a paused database is also closed, its Backups page has Export data in place of Back up now: to get its data out at any time, export it, as often as you like. Its automatic backups don't run while it is paused. Upgrade your plan, or delete a database you don't need, to make room.

Closed databases

A database is closed while:

  • your account is suspended, whatever the reason;
  • it is paused over the plan's database limit;
  • your plan doesn't include databases: once a downgrade's grace period ends, or at once when a subscription pause leaves the account on a plan without them;
  • it is a WordPress site's own database and the site is paused, unless another app is connected to it for Both or Production only (a Preview only connection doesn't count).

Closed means apps can't connect: the logins of its users are refused and open connections are ended. Nothing is deleted: its data and backups are kept. It opens again by itself once none of these holds, before its apps start again. The database's page says why it's closed, the databases list and the app's connected databases mark it Closed, and the API returns closed: true with a closed_reason.

Pausing a single app never closes a database, because other apps may still use it. When your plan doesn't include databases, an app connected for Both or Production only is paused too; Preview only never pauses an app.

Get your data out. While a database is closed, export it from its Backups page, where Export data takes the place of Back up now: as often as you like, and your plan doesn't need to include databases or backups. Through the API, a closed database also takes one backup while it's closed if your plan includes database backups (a failed one doesn't count). After that backup, or after an export, a new backup is refused (409 database_closed) until the database opens again: export it instead. While the account is suspended, whatever the reason, no backup can be taken or downloaded and nothing can be exported: the data comes back with the account.

Backups

A database's backups belong to the database, not to an app: every database has them, whether or not an app uses it. A backup is a gzipped SQL dump of the whole database (its tables, data and views). Open the database (Storage → Databases → the database) and its Backups page.

  • Back up now. Takes a backup of the database. It shows as running and the page updates by itself until it has finished. One backup of a database runs at a time, and the database has to be Ready.
  • The list. Each backup shows how it was taken (On demand or Automatic), when (Taken), its Size and its Status, with a restore status once it has been restored. Long lists are split into pages.
  • Download. Downloads the dump (.sql.gz). Because the dump is the database's data, you need the Database data permission (see Permissions). The dump has no DROP TABLE statements: import it into an empty database.
  • Restore… Replaces all of the database's data with the backup; anything written after it was taken is lost. Type the database's name to confirm. The dialog lists every app connected to the database: a restore affects all of them. They keep running and see the restored data as soon as the restore finishes; while it runs, the database's tables are dropped and imported again.
  • Delete. Deletes a completed or failed backup. It can't be recovered.
A restore affects every connected app. There is no undo. Take a fresh backup first if you might want to come back.

Automatic backups

The Automatic backups card on the same page backs the database up on a schedule: switch it on, pick Daily or Weekly (every 7 days from the first run), the Time (UTC), and how many to keep (Keep the last N), then Save schedule.

  • Each automatic backup deletes the database's oldest completed backups beyond the newest N — backups you took on demand count too.
  • N is capped by your plan's retention limit (backup_retention_max); the card shows it.
  • An automatic backup is skipped while the database isn't ready, is paused, or already has a backup running; the schedule moves on to its next run.
  • Whatever the schedule, ngris keeps at most 50 completed backups of a database.

Export a closed database

A closed database (paused over your plan's limit, kept when your plan no longer includes databases, or a paused WordPress site's own database) has an Export data card at the top of its Backups page. On the Databases page, the database's row menu has Export data too.

  • Export data takes a copy of all of the database's data: a gzipped SQL dump (.sql.gz), like a backup. The card shows Exporting… and the page updates by itself; once it's Ready, click Download.
  • Export as often as you like while the database is closed, with Export again. Your plan doesn't need to include databases or backups.
  • Each export replaces the previous one, and a backup taken through the API while the database was closed: only the latest copy is kept. The data can't change while the database is closed, so the latest export has all of it. Automatic backups, and backups taken before it closed, are kept as they are.
  • You need the Database data permission to export and to download (see Permissions).
  • One backup or export of a database runs at a time. While the database is still being paused, the card says so: export it once it's paused. An open database has no Export data card: back it up instead.
  • While the account is suspended, nothing can be exported.

Plans

Backups use the plan's backup features: application_db_backup (take a backup), application_db_restore (restore one), application_backup_schedule (turn automatic backups on) and backup_retention_max (how many automatic backups are kept). On a plan without database backups, or without databases, the page shows what upgrading unlocks; a closed database's Backups page still offers Export data. If the plan later loses automatic backups, ngris turns the schedules off; turn them back on after upgrading.

WordPress sites

A WordPress site's own database is listed under Storage → Databases too, and on a plan with databases its Backups page holds backups of the database alone. The site's full backups (database and files) and content backups (files) stay on the site's own Backups page — see Application Backups.

API

curl -X POST "https://api.ngris.com/v1/databases/{uuid}/backups" \
  -H "X-API-KEY: <your_api_key>"

See Database Backups (list, back up, download, restore, delete), Database Backup Schedule and Export a Paused Database.

Delete a database

Open the database's Settings page and click Delete database, then type DELETE to confirm. ngris deletes the database, its users, all of its data and all of its backups: first the backups (after any backup or restore in progress has finished), then the database. This can't be undone. Download any backup you want to keep before you delete the database.

Deleting is refused while any app uses the database: the Settings page lists the apps, and the API answers 409 database_in_use with them. Disconnect each app first.

When you delete an app

Deleting an app removes its connections. Every database it used stays, with its users and data — one you created under Storage → Databases, and also a per-app database added from the app's own Database page — and you can connect it to another app or delete it. A database that stays still counts toward your plan's database limit until you delete it. Only a WordPress site's own database is deleted with the site.

The app's own backups (a WordPress site's content and full backups) are deleted with the app. A database's backups belong to the database: they stay with every database that stays. A WordPress site's own database is deleted with the site, with its backups.

Per-app databases (an app's Database page)

A managed, full-stack or function app can still add a database from its own Database page (Add database, or POST /v1/applications/{uuid}/database). It's a shortcut: ngris creates an ordinary database under Storage → Databases, named after the app (with -2, -3 … when you already have a database of that name), with a read-write user named app, and connects the app to it as app with no prefix:

  • the app gets DB_HOST … DATABASE_URL, shown as locked database rows on its Environment page — nothing is written into its own environment variables;
  • no password is shown when it's created: if you need one, reset the password of the app user (the app restarts);
  • it's an ordinary database: you can rename it, add users, connect other apps to it, or disconnect it;
  • it stays when you delete the app, with its users, data and backups;
  • its backups are on its own Backups page, like any database's.

An older per-app database that hasn't been moved onto a connection shows its app as connected (managed by the app): it can't be disconnected there, and it can't be deleted while the app exists.

A WordPress site gets its own database when it's created. That one belongs to the site: it's listed under Storage → Databases, doesn't count toward your plan's database limit, can't be disconnected, and is deleted with the site.

An app with a database from its Database page already has the unprefixed names, so connect any other database to it with a prefix.

Permissions

Account roles have a Databases permission (see named-role RBAC):

  • Read — see the databases, their users, connections, backups and backup schedule.
  • Write — create databases and users, and take backups.
  • Update — rename a database, reset a user's password, restore a backup, and change the backup schedule.
  • Delete — delete databases, users and backups.

A separate Database data permission (read) covers the data itself: it is needed to download a backup, also a database dump downloaded from an app's Backups page, and to export a closed database and download the export. The account owner and the built-in Admin role have it; custom roles that existed before databases got their own permissions have it off until an admin turns it on.

Connecting a database to an app also changes the app, so it needs Endpoints permission too: listing an app's connections needs Databases read and Endpoints read; connecting needs Databases write and Endpoints write; disconnecting needs Databases delete and Endpoints write. The account owner has every permission.

Troubleshooting

  • The database failed to create. Delete it in Settings and create a new one; no partial database is left behind.
  • “These environment variable names are already used by the application”. The app already has one of the names, often DB_HOST … DATABASE_URL from a database on its Database page or from another connection. Connect with a prefix.
  • “This application type can't use a database”. Only managed, full-stack and function apps can connect. See Who can connect.
  • The app can't connect. Almost always the wrong variable or format: DATABASE_URL (a mysql:// URL) for Node, Prisma and SQLAlchemy, a user:pass@tcp(host:port)/db DSN built from the DB_* variables for Go. Check the prefix too. See Use the connection in your code.
  • “Access denied”. Read the password from the environment at runtime; a password you copied or baked into the image stops working after a reset. Right after a reset, wait for the app's restart to finish.
  • Inserts and updates fail with “command denied”, but reads work. The database is over its storage limit and its writes are blocked. Delete data or drop tables you don't need; writes come back under 95 % of the limit. See When a database reaches its storage limit.
  • A CREATE TABLE with ENGINE=MyISAM (or another engine) fails. Only InnoDB tables can be created; drop the ENGINE= clause or set ENGINE=InnoDB.
  • The variables are marked pending. The database or its user isn't ready yet. They reach the app once both are ready, and ngris restarts the app.
  • You can't delete a user or the database. An app still uses it. Disconnect the app first (the error and the dialog list which).
  • There is no Download on a backup. Your role lacks the Database data permission; ask an admin.
  • There is no Export data card. The database is open: back it up instead. Export data is only on a closed database's Backups page, where it takes the place of Back up now.
  • Restore is missing or Back up now is disabled. The database isn't ready, a backup or restore is running, it is paused (no restore), or your plan doesn't include it; the page says why.

Still stuck? See Deploy troubleshooting.

Was this helpful?