Skip to content

Deployment · By Rivtor · Updated

Deploy an Express API with Postgres

An Express API needs a production start command, a reachable network listener and a Postgres connection read from the environment. Test an actual database read and write after deployment.

See the Rivtor deployment workflow →

Declare the production entrypoint

Add a start script to package.json and include runtime packages in dependencies. For TypeScript, build the JavaScript before launching it. Development watchers are useful locally but should not be assumed to exist in the runtime image. Confirm the entrypoint starts from the service directory.

Use a bounded connection pool

Create one pg Pool for a running application process and read DATABASE_URL from the environment. Do not create a new pool in every request. Release checked-out clients in finally blocks, parameterize SQL input and set query timeouts. Multiply connection budgets across all application instances.

Check routing and health

Bind the application to the configured service port and a reachable interface. A loopback-only listener inside the container cannot receive normal router traffic. Use a lightweight health route without browser authentication redirects, and test a normal authorized API endpoint as a separate check.

Test persistence and error handling

Create and read synthetic data after release. Check that an invalid request returns a controlled error and logs do not contain passwords. The Rivtor dashboard also includes an Express + Postgres starter for exploring this workflow before you connect an existing application.

Command reference

npx rivtor deploy --dry-run
npx rivtor deploy
npx rivtor logs --since 1h

These commands illustrate the documented workflow. Replace example addresses and identifiers with your own. Deployment and migration commands affect the selected project; confirm the target before running them.

Minimal server and connection pool

An Express API should expose a production start script and a listener reachable by the platform router. The following ES-module example uses express and pg:

import express from "express";
import pg from "pg";

const app = express();
const pool = new pg.Pool({
  connectionString: process.env.DATABASE_URL,
  max: 5,
  connectionTimeoutMillis: 5000,
  idleTimeoutMillis: 30000,
});
pool.on("error", () => console.error("Idle database connection failed"));

app.get("/health", async (_request, response) => {
  try {
    await pool.query("SELECT 1");
    response.json({ ok: true });
  } catch {
    response.status(503).json({ ok: false });
  }
});
app.listen(Number(process.env.PORT || 3000), "0.0.0.0");

Set type to module in package.json and point the start script at this file. This health-only example does not implement business routes or schema migrations. Its pool limit is an example budget, not a universally suitable value. Total possible connections increase with replicas and separate worker processes.

Keep application queries safe

Use parameterized queries for user input. A query such as SELECT ... WHERE id = $1 with a separate parameter array avoids constructing SQL from request text. Authentication and record-level authorization still need application logic.

For multi-statement transactions, acquire one client with pool.connect(), execute BEGIN and all statements on that same client, then COMMIT or ROLLBACK and release in a finally block. Separate pool.query calls may use separate connections. A leaked checked-out client can exhaust the pool even when the API process remains alive.

Release checklist and expected observations

Before deployment, run the package's production start command from its own directory. Confirm the database connection is configured in the server environment, review migrations and inspect the detected service in Rivtor's dry-run plan.

After deployment, expect the health endpoint to return a successful HTTP status and an ok field when its database query succeeds. Separately exercise a synthetic application's create/read flow. A 503 should lead to private server logs and connection settings, not an error response containing DATABASE_URL.

If no route responds, inspect the listener and port troubleshooting guide. If tables are absent, inspect migration failures. When adding asynchronous jobs, use the BullMQ worker guide to account for another process and connection budget.

Sources: Express production practices, node-postgres pools, and parameterized queries.

Next steps

Read the Rivtor reference for supported configuration and limitations, and use the deployment quickstart when trying Rivtor for the first time. Validate workflows with synthetic data before connecting valuable production records.