Skip to content
Docs

Connect an app to a database

Attach a database to an app, and its host, port, user, password and address become its variables, with the names Laravel and most frameworks read.

Updated 17 Oct 20263 min read

You don't type connection details on Tanzahost. When a database is attached to an app, its host, port, database, user, password and a connection address are added to the app's variables, and the app redeploys with them without downtime. They use the names Laravel and most frameworks already read.

Attach a database

When you add a database, tick the apps that should get its connection details. To attach one later:

  1. Open the database from your project's Overview.

  2. Under Apps with its connection details, switch on the app.

The app redeploys with the new variables, side by side with the running version, so visitors see no downtime. Switch it off to detach: the variables go, and the app redeploys again.

The variables

DatabaseVariables your app gets
MySQLDB_CONNECTION=mysql, DB_HOST, DB_PORT=3306, DB_DATABASE, DB_USERNAME, DB_PASSWORD, DATABASE_URL
MariaDBThe same, with DB_CONNECTION=mariadb
PostgreSQLThe same, with DB_CONNECTION=pgsql, DB_PORT=6432, and DATABASE_DIRECT_URL as well
ValkeyREDIS_HOST, REDIS_PORT=6379, REDIS_PASSWORD, REDIS_URL, and Laravel's cache or queue settings

The connection addresses look like this, with the real password where the dots are:

Terminal
DATABASE_URL=mysql://db_duka:…@mysql.duka.internal:3306/db_duka
DATABASE_URL=postgres://db_duka:…@postgres.duka.internal:6432/db_duka
REDIS_URL=redis://default:…@cache.duka.internal:6379

For PostgreSQL, DATABASE_URL goes through connection pooling, and DATABASE_DIRECT_URL (port 5432) doesn't. See PostgreSQL. For Valkey, see Valkey.

The host name

A database is reached at its name, then your project's name, then .internal. Asha's MySQL named mysql in project duka is mysql.duka.internal. That name works only inside duka: an app in another project can't reach it, and nothing on the internet can. Connections from outside, when you switch them on, use a different address (see below).

Managed variables

On the Variables tab, these variables show as added by the database, and they can't be changed by hand. That's on purpose: when the password changes, every attached app gets the new one by itself. Change them on the database's page instead.

If you had typed a value for one of those names before attaching, like your old host's DB_HOST, it's kept aside, sealed, and comes back when you detach the database. If two databases of the same kind are attached to one app, the one attached first fills the names; detach it, and the other one's values take their place.

Example: Asha's Laravel app

Asha's .env on her old host had six DB_ lines. On Tanzahost she leaves them out. Her app web gets them from the attached database, and Laravel's config/database.php reads them as it always has. Her .env.example stays in Git without any password in it.

For a Node.js app, read the single address:

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

For Python, read it from the environment:

python
import os
url = os.environ["DATABASE_URL"]

Connecting by hand

The database's Connect section shows the host, port, database, user and password (the eye button shows the password), each with a copy button. The full connection address is in the attached apps' DATABASE_URL (REDIS_URL for Valkey). Use them in an app's terminal, for example with mysql or psql, or for an app that needs a variable name of its own. Copy connection address, in the database's menu, copies its host and port.

To look at your data, use the database's own page, which lists the tables and runs queries. To use a tool on your own computer, like TablePlus or DBeaver, switch on Connections from outside for an own database: it gets an address of ours, like db-7h3k2q.tanzahost.co, and only encrypted connections get through. It's off until you switch it on. See Connect from your own computer.

When it doesn't connect

  • The app was deployed before the database was ready. Wait until the database shows Running, then redeploy the app.

  • The app reads other names. Some apps want, say, MYSQL_HOST. Add a variable of your own and copy the value from Connect, but remember to update it after a password change.

  • The app is in another project. Databases are private to their project. Add the database in the app's own project instead.