# Deploy NestJS with PostgreSQL from Claude Code

Source: https://resilis.io/guides/nestjs-postgresql-claude-code/

Updated date: 2026-11-19

You have a NestJS API that works on your machine. This guide takes it to a running service with a managed PostgreSQL database, without leaving your Claude Code session. Claude Code edits the source. Resilis builds it, runs it next to the database, and keeps every release ready to roll back. You will:

## Overview

- 1. Prepare the NestJS app for a container: port, host, database URL and migrations.
- 2. Connect Claude Code to the Resilis MCP server.
- 3. Create a private PostgreSQL database and bind it to the app as a secret.
- 4. Deploy the local workspace, then run migrations as a release command.
- 5. Check the live API and know how to roll back.

## Before you start

- A Resilis account with access to the organization you want to use. Connecting to the MCP server does not need a paid plan. Running the app and database is billed against your plan's runtime credit.
- Claude Code installed, with your NestJS project open as the working directory.
- Git installed. The workspace route uses Git and a private repository hosted by Resilis. You do not need a GitHub repository.
- A NestJS app using TypeORM with PostgreSQL. The same steps work with another ORM if you swap the migration command.

## Prepare the app for a container

Three things trip up most first NestJS deployments: the listen address, the database configuration and schema changes at startup.

## Listen on 0.0.0.0 and the configured port

The default NestJS bootstrap listens on process.env.PORT ?? 3000. Inside a container, also bind to 0.0.0.0, or requests from outside the container cannot reach it.

### TypeScript

```ts
// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableShutdownHooks();
  await app.listen(process.env.PORT ?? 3000, '0.0.0.0');
}
bootstrap();
```

## Read the database from DATABASE_URL

Resilis binds a managed database to your app as one sensitive environment variable, DATABASE_URL by default. Read it in one place and turn schema sync off. The NestJS documentation is explicit: don't use synchronize: true in production, or you can lose production data.

### TypeScript

```ts
// src/data-source.ts — used by the app and by the TypeORM CLI
import { DataSource, DataSourceOptions } from 'typeorm';

export const dataSourceOptions: DataSourceOptions = {
  type: 'postgres',
  url: process.env.DATABASE_URL,
  entities: [__dirname + '/**/*.entity.js'],
  migrations: [__dirname + '/migrations/*.js'],
  synchronize: false,
};

export default new DataSource(dataSourceOptions);
```

### TypeScript

```ts
// src/app.module.ts (excerpt)
TypeOrmModule.forRoot(dataSourceOptions),
```

## Add migration scripts and a health endpoint

The TypeORM CLI runs compiled .js migrations, so point it at the built data source:

### package.json scripts

```json
{
  "scripts": {
    "build": "nest build",
    "start:prod": "node dist/main",
    "migration:run": "typeorm migration:run -d dist/data-source.js",
    "migration:revert": "typeorm migration:revert -d dist/data-source.js"
  }
}
```

Add a small GET /health route that returns 200 once the app has started. You will use it to check the live service.

## Choose a build route

Resilis builds with Railpack automatically, or from your Dockerfile. A Dockerfile keeps the NestJS build explicit:

### Dockerfile

```dockerfile
FROM node:22-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev

FROM node:22-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/package*.json ./
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
EXPOSE 3000
CMD ["node", "dist/main"]
```

If you prefer Railpack, ask the agent to set the start command to npm run start:prod, and try the build in your development environment before you rely on it.

Keep the TypeORM CLI available in the runtime image if you run migrations from it: typeorm must be a production dependency, not a dev dependency, or npm prune removes it.

## Connect Claude Code to Resilis

Register the remote server and sign in. These are the same commands as the Claude Code setup guide.

### Shell

```sh
claude mcp add --transport http resilis https://mcp.resilis.io
claude mcp login resilis
claude mcp list
```

claude mcp list should show Resilis as connected. Inside a session, /mcp shows the server and its tools. During sign-in, review the access Claude Code requests and pick the organization for this project.

## Ask for a plan before anything changes

Start with a request that keeps you in control:

### Example

```text
Inspect this NestJS workspace and prepare a Resilis deployment plan.
List my projects and environments, and ask me which project to use.
Propose a private PostgreSQL 17 database in the development environment,
bound to the app as DATABASE_URL. Show the build strategy, start command,
port and required variables. Do not create anything until I approve.
```

Behind that request, the agent calls read-only tools such as capabilities.get, projects.list and environments.list. If you need a new project, projects.create creates it with development, staging and production environments already in place.

## Create and bind the database

After you approve the plan, the agent calls databases.create with the PostgreSQL engine in the project and environment you chose. Keep public access off: the app reaches the database over the internal connection.

Then it calls databases.bind. This injects the connection string into the app's environment as a sensitive variable. The credential never passes through the model's context, so it cannot end up in a transcript, a commit or a log line the agent prints.

Place the app and database in the same project, environment and region. An internal database address in one environment may not resolve from another.

## Deploy the workspace

Ask the agent to deploy:

### Example

```text
Deploy the reviewed commit from this workspace to the development
environment of this project. Use the Dockerfile. Show me which files
will be committed before you push.
```

The agent calls workspaces.deploy. The tool walks it through each local step, one at a time:

- 1. Check that Git is installed and the folder is a repository.
- 2. Review tracked, untracked and ignored files. Credentials, .env files, keys, node_modules and dist stay out.
- 3. Commit, and report the exact commit SHA.
- 4. Push to the private Resilis-hosted repository.

Resilis then compares the hosted branch head with the reported SHA. Only a matching commit is built. If they differ, the agent is asked to push again.

The agent follows progress with deployments.observe, which returns status plus a bounded slice of build or runtime logs. Named build contexts are not supported on the workspace route.

## Run migrations as a release command

Run schema changes as their own step, not inside the app's start command. A release command uses the exact image and bound environment of the current deployment, with a timeout and its own logs:

### Example

```text
Run "npm run migration:run" as a release command on the current
development deployment. Show me the exit code and the last 50 log lines.
```

The agent calls deployments.release.run with ["npm", "run", "migration:run"], then deployments.release.get and deployments.release.logs. The default timeout is 900 seconds.

## Check the live API

Ask for the app URL with apps.get, then check it yourself:

### Shell

```sh
curl -i https://<your-app-url>/health
```

Create and read a disposable record through the API. A Running status and a green build are checkpoints. A real request against real data is the proof.

If something fails, read the first actionable error. Build failures show in logs.build. Startup and connection errors show in logs.runtime. Common causes: the app listening on localhost, a missing variable, or the database in a different environment.

## Ship the next change and roll back

Each deploy keeps the previous release. If a change misbehaves, the agent can return to a known deployment with deployments.rollback, or you can use Rollback in the dashboard.

Rollback changes the application release. It does not undo migrations or restore data. Write migrations the previous release can still run against, such as adding a column before you start reading it. For data mistakes, see recovering from a bad Postgres migration.

## Promote to production

Repeat the database and deploy steps in the production environment of the same project. Production is where the careful parts matter most:

- Deleting or stopping a production resource waits for an emailed approval from an account owner.
- An agent cannot delete anything directly. It can prepare a deletion; a person approves it. See letting an agent deploy without the delete key.
- On Pro, turn on continuous backups for the production database so it can be restored to a point in time.

## What it costs

Hobby is $10 per month and Pro is $50 per month. The plan fee becomes runtime credit for CPU, RAM, disk, database fees, egress and object storage. Each database adds a service fee: $5 per month on Hobby, $10 on Pro, prorated hourly. The billing docs include a worked example for one app and one PostgreSQL database.

## Frequently asked questions

Do I need GitHub to deploy a NestJS app from Claude Code? No. The workspace route pushes a reviewed commit to a private Git repository hosted by Resilis. You need Git installed, not a GitHub repository.

Does Claude Code ever see my database password? Not when it uses databases.bind. The connection string is written into the app's environment as a secret and is not returned to the model.

How should I run TypeORM migrations in production? Keep synchronize off and run typeorm migration:run against the compiled data source as a release command on the current deployment. You get an exit code and logs for every run.

Does rolling back the app undo a migration? No. App rollback selects an earlier release. Database recovery is separate: snapshots on Hobby, and point-in-time restore on Pro and Enterprise.

Which NestJS versions are supported? Resilis runs the container your build produces, from Railpack or your Dockerfile. Test your exact versions in a development environment first.
