Naijacloud
Docs

Connecting to a database

Your services reach a database over the internal network, using details the platform gives you. You can also connect from your own machine. The one thing you must get right in your own code is pooling.

5 min read

Getting the connection details

Open the database and choose Connect. You get the host, port, database name, user and password, and a ready-made connection string.

A database overview listing its host, port, database, user, password and connection URL, each with a copy action

Copy the details from there rather than writing them by hand. The host differs from what you would guess, and the password is generated.

Use the internal host

A database is reachable by your own services over the internal network. Use the internal host in your application's connection string: traffic stays inside the region and never crosses the public internet.

Environment variable
bash
DATABASE_URL=postgresql://USER:PASSWORD@HOST:PORT/DATABASE

Set it as an environment variable on the service, scoped to the environment that should use that database. The platform does not set it for you. The quickest way is the app's Variables tab: it lists the databases in the environment, and Add as DATABASE_URL saves the internal URL for you. See Environment variables. Never commit it to the repository.

Each environment should point at its own database. A dev service holding the prod connection string is how test data ends up in production reporting.

Copy it for your framework

Copy for… on the Connection card writes the connection the way your framework expects it: Prisma, Drizzle, TypeORM, Django, Laravel, Rails, or a plain Node driver (pg for PostgreSQL, mysql2 for MySQL and MariaDB). Each snippet says where it goes, usually your .env plus a few lines of config.

The SSL setting follows the endpoint on screen. The private endpoint is a plain connection on the project's network, so SSL is off. The public endpoint only accepts TLS, so every snippet for it turns SSL on (sslmode=require in the URL, or ssl: true for Node drivers).

Moving from Supabase

Supabase's direct database host (db.<project>.supabase.co) only has an IPv6 address. Naijacloud's network is IPv4, so an app pointed at that host times out before it connects. When a deploy fails this way, the deploy page names the variable and links here.

You have two fixes:

  • Keep the database on Supabase. In Supabase, open Connect and copy the Session pooler connection string. Put it in the variable your app reads. The pooler host is reachable over IPv4.
  • Move the database to Naijacloud. Create a PostgreSQL database in the same environment, restore a pg_dump of your Supabase database into it, and point DATABASE_URL at the new internal URL. Supabase is left untouched.

If your app sets both DATABASE_URL and SUPABASE_DB_* variables, check which one your code reads.

Pooling is not optional

Every database instance has a maximum number of connections, and it is much smaller than the number of requests your app will serve. A pool holds a small number of connections open and lends them out.

Create the pool once, at module scope, and reuse it:

db.js
js
import { Pool } from 'pg'; // One pool for the process, not one per request. export const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 10, idleTimeoutMillis: 30_000, });
Using it
js
import { pool } from './db.js'; export async function getOrder(id) { const { rows } = await pool.query('select * from orders where id = $1', [id]); return rows[0]; }

Creating a client inside a request handler opens a connection per request. Under any real traffic the instance's connection limit is reached, and every subsequent query fails, including the ones from healthy code. If the Connections figure on the overview climbs and does not come back down, this is why.

Size the pool against the instance limit, and remember that each running copy of your service has its own pool: several services sharing one database each need a share of the total.

A database accepts 20 connections on Free, 50 on Dev, 100 on Pro, 150 on Pro Max and 200 on Pro Ultra.

The connection pooler

When many copies of a service, or many short-lived processes, share one database, an app-side pool is not enough on its own: each copy holds its own connections, and together they reach the limit. Paid databases can switch on a connection pooler: a PgBouncer in transaction mode, on the project's private network, at no extra charge.

Open the database's Settings and switch on Connection pooler. The database then has a second variable, DATABASE_POOLER_URL. Put it in the apps that should use it, as you do DATABASE_URL. Hundreds of app connections then share a small set of database connections: on Pro Max, up to 120, with 30 kept free for direct use.

In transaction mode a connection goes back to the pool at the end of each transaction, so keep DATABASE_URL for anything that needs a session of its own:

  • migrations;
  • LISTEN / NOTIFY;
  • advisory locks;
  • SET without LOCAL, and temporary tables.

Prepared statements work through the pooler, so ORMs need no special flag. With Prisma, set url to DATABASE_POOLER_URL and directUrl to DATABASE_URL, so migrations go straight to the database.

The pooler logs in with the database's password. If you change that password with SQL, switch the pooler off and on again so it picks up the new one.

Working with it yourself

For quick queries, use the built-in editor: checking data, running a migration by hand, inspecting a schema. It talks to the database over the internal network, so nothing has to be exposed to reach it.

See The database editor.

From your own machine

A PostgreSQL database can be reached from outside Naijacloud, so you can use psql, pg_dump, a GUI client or your ORM's studio from your own computer. The public endpoint is off by default. To turn it on, open the database's Settings, find Public access, choose who can connect, and save:

  • Specific IPs. Only the addresses on your allowlist can connect. Prefer this.
  • Allow all. Any address can try to connect, and only the password protects the database.

Once it is on, Connect shows a second, external connection string. Clients connect over TLS. Your services should keep using the internal one, which is faster and never leaves the region. Public access works in every region.

MySQL, MariaDB, MongoDB, Redis and Valkey are reachable from your services only.

Migrations

Run migrations from your application's own tooling, as part of a deploy or as a cron job. Two things to keep in mind:

  • Make migrations safe to run twice. A deploy can be retried.
  • Roll out schema changes so the previous version of the code still works against the new schema. Add a column before you require it, and remove it a release later. Otherwise a rollback breaks against a schema it cannot read.

Related: Preview deploys.