Add a database

Add a PostgreSQL or MySQL database that Ghaym runs for you, and let the project's services reach it.

Add the database

  1. Open the project, press ⌘K and choose Add service › PostgreSQL or Add service › MySQL. For Redis, add a cache instead.

    With the CLI:

    Terminal
    ghaym cloud services create --project shop --type database --engine postgres --name db --db-size <size> --site <slug>

    For MySQL, pass --engine mysql.

  2. Pick the version, then type the name and press Enter. Then pick the site the database runs in, which can't change later, and the size if there is more than one.

    PostgreSQL comes in 16, 17 and 18; 18 is picked first. MySQL runs 8.4. ghaym cloud engines list lists the engines Ghaym runs and their versions. Both engines come in 512 MB and 1 GB, at the same price per size.

Reach it from your services

A database's values reach a website only through a variable the website shows. Set the variable to a reference: ${{db.DATABASE_URL}} reads the DATABASE_URL of the database named db. On the database's page, choose Copy as reference in a variable's menu to copy that form.

PostgreSQL provides DATABASE_URL, DATABASE_READER_URL, PGHOST, PGPORT, PGUSER, PGPASSWORD and PGDATABASE.

MySQL provides DATABASE_URL, DATABASE_READER_URL, MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD and MYSQL_DATABASE, on port 3306. Its DATABASE_URL starts with mysql://. MySQL keeps the names mysql, sys, performance_schema and information_schema for its own databases. Name a MySQL database after one of them, with hyphens for underscores, and Ghaym adds _db to the name of the database inside it: mysql gets mysql_db, and performance-schema gets performance_schema_db. MYSQL_DATABASE holds that name.

While a website references none of a database's variables, its variables page says db isn't wired to this service. Choose Add DATABASE_URL there to stage the reference in one step.

A website you deploy from a repo whose .env.example names DATABASE_URL, in a project with one PostgreSQL or MySQL database, starts with that reference set. With two databases, Ghaym leaves DATABASE_URL for you to set.

With the CLI, set the reference on a website yourself:

Terminal
ghaym cloud vars set DATABASE_URL '${{db.DATABASE_URL}}' --project shop --env production --service web

Check it

Open the database. Under Connection, Inside the project shows the host, port, database name and user your services use, none of it secret. To open the database from your laptop, connect through a tunnel.

Terminal
ghaym cloud services connection db