Deployment · By Rivtor · Updated
Deploy Fastify with PostgreSQL and a bounded connection pool
A deployed Fastify API needs a reachable listener, a production entrypoint and a bounded PostgreSQL pool. Open connections through the server environment, close them during shutdown and verify a real query rather than relying only on process startup.
See the Rivtor deployment workflow →
Make the listener reachable
Fastify development examples often run only on localhost. Container traffic arrives from outside the process, so use the interface and assigned port required by the host. Include Fastify and the database driver in the service’s runtime manifest.
This illustrative ESM entrypoint checks database connectivity. It does not create tables, authenticate users or provide a complete application:
import Fastify from 'fastify';
import pg from 'pg';
const app = Fastify({ logger: true });
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL, max: 3 });
app.get('/ready', async () => {
await pool.query('SELECT 1');
return { ok: true };
});
app.addHook('onClose', async () => { await pool.end(); });
await app.listen({ host: '0.0.0.0', port: Number(process.env.PORT ?? 3000) });
Use an appropriate module configuration and supported Node runtime. For TypeScript, compile or package the production entrypoint deliberately; a local development runner must not be an accidental dependency.
Budget connections and review migrations
The pool maximum applies per process. Multiply it by the largest number of API and worker processes that can overlap during a release. Leave capacity for migrations and operator access. Parameterize application queries and release manually checked-out clients in finally blocks.
Keep migration execution separate from ordinary request handling. Provisioning PostgreSQL does not create the application’s tables. Review Rivtor’s framework and data wiring and confirm the migration runs from a directory containing its configuration.
Exercise the service boundary
Check the health path, then an authenticated database-backed request using synthetic records. Restart the API and read the same record. A SELECT 1 health response proves a connection works but cannot establish that the business schema is correct.
For failure diagnosis, separate a process that never listens, a health route at the wrong path and a request waiting for a database connection. Inspect status codes and sanitized logs for each. Increasing timeouts without locating the blocked resource can hide a connection leak rather than repair it.