All documentation

Express and PostgreSQL

An Express API with a managed PostgreSQL database: connecting via DATABASE_URL, the port and migrations.

1. Database

“New resource” → “Database” → PostgreSQL 16 → plan. The password is shown once — save it if you will connect to the database from anywhere other than the app.

2. App

“New resource” → “App”, the link to a repository with package.json. The platform runs npm ci, npm run build (if the script exists) and npm start.

The database and the app must be in the same data center: a database is visible only to resources on the same server. The platform places resources of one project together when the server has room.

3. Connection

Open the database → “Connection” tab → choose the app → “Connect”. The connection string arrives in the app's DATABASE_URL variable and is updated automatically when the database password changes. No need to type it in by hand.

Code

import express from 'express';
import pg from 'pg';

const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const app = express();
app.use(express.json());

app.get('/api/items', async (req, res) => {
  const { rows } = await pool.query('select id, title from items order by id');
  res.json(rows);
});

const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0');

Listen on the port from PORT on 0.0.0.0: a deploy waits for the port to respond and only then switches traffic.

Migrations

Add a Procfile to the repository root with a release: line — it runs before the app starts, on every start:

web: node server.js
release: npx knex migrate:latest

Any tool works: npx prisma migrate deploy, npx drizzle-kit migrate, node migrate.js. The command must be idempotent — running it again with no new migrations changes nothing. If a migration fails, the new version does not start, the deploy is marked as failed and the previous version keeps running. More in Several processes in one app.

Backups

Enable a backup plan in the database's “Backups” tab: a backup includes the data volume and a pg_dump export. See Managed databases.