Skip to main content

Express Middleware

PG Dashboard exports an Express router factory:

const { pgDashboard } = require("pg-dashboard");

Mount it under any route:

app.use("/admin/db", pgDashboard({ connectionString }));

The dashboard serves both its internal API and the React app under that route.

Internal Route Order

The router is ordered like this:

  1. API routes under /api/*
  2. Static React assets from frontend/dist
  3. SPA fallback returning index.html

This is what allows direct reloads such as /admin/db to keep working.

Protecting The Dashboard

The package does not include authentication. Protect the mounted path yourself:

function requireAdminUser(req, res, next) {
if (!req.user?.isAdmin) {
res.status(403).send("Forbidden");
return;
}

next();
}

app.use(
"/admin/db",
requireAdminUser,
pgDashboard({
connectionString: process.env.DATABASE_URL,
}),
);

Using An Existing Pool

If your app already manages a pg pool, pass it directly:

const { Pool } = require("pg");
const { pgDashboard } = require("pg-dashboard");

const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 10,
});

app.use(
"/admin/db",
pgDashboard({
pool,
schemaName: "public",
}),
);

When pool is provided, PG Dashboard does not create its own pool.