Host a Node.js app: Next.js and PostgreSQL in production

To host a Node.js app, you run a process around the clock that answers requests, next to its database, on a server that restarts it on its own and updates without interrupting service. This guide follows a Node.js or Next.js app with PostgreSQL into production, step by step, based on the official documentation.

Simon Vasconcelos Lee By Published on 27 November 20269 minute read

All guides

In short

  1. In production, choose an LTS version of Node.js: Node.js 26 has been in Active LTS since 28 October 2026, maintained until 30 April 2029.
  2. Hand the process to systemd or PM2: they restart it after an error and when the server boots.
  3. Put Nginx in front of the app, which listens locally only, and install the HTTPS certificate with Certbot.
  4. Keep secrets out of the repository and out of NEXT_PUBLIC_ variables, which end up in the browser’s JavaScript.
  5. Plan for 4 GB for an API and its PostgreSQL database, 6 to 8 GB if Next.js is built on the server.

The foundation: what runs a Node.js app

In production, the app becomes a service: a permanent Node.js process, a web server in front of it, a database beside it, and the means to restart, log and update the whole. That is what a server dedicated to the app brings: permanent processes, a database tuned for it, background jobs and reserved resources.

ComponentRoleCommon choice
Node.jsRuns the appAn LTS version
Process managerStarts and restarts the app, keeps its logssystemd or PM2
Reverse proxyReceives traffic, holds the HTTPS certificateNginx
CertificateEncrypts trafficLet's Encrypt, renewed by Certbot
DatabaseKeeps the dataPostgreSQL, on the same server at launch
FirewallOpens SSH, HTTP and HTTPS, and closes the restufw or nftables

Each line is installed once, then maintained. On a Managed VPS, the host looks after this foundation for you and proactively monitors the app: it spots errors, slowdowns, expensive queries and shrinking disk space, then recommends the fix, which you apply, yourself or through your AI assistant.

Which version of Node.js

The Node.js documentation reserves production for LTS versions ("Active LTS" or "Maintenance LTS"), which as a rule receive critical fixes for 30 months in total. In November 2026:

VersionStatusEnd of maintenance
Node.js 26Active LTS since 28 October 202630 April 2029
Node.js 24Maintenance LTS since 20 October 202630 April 2028
Node.js 22Maintenance LTS30 April 2027

A new project starts on Node.js 26; Next.js requires Node.js 20.9 at minimum, which all three versions exceed. From Node.js 27 on, the rhythm becomes yearly: one major version a year, each moving to LTS after six months.

systemd or PM2: keeping the app running

Started by hand, a process stops with the SSH session. Two tools take over:

File /etc/systemd/system/my-app.service (Next.js with standalone output)
[Unit]
Description=My Next.js app
After=network.target postgresql.service

[Service]
User=my-app
WorkingDirectory=/srv/my-app/current/.next/standalone
Environment=NODE_ENV=production PORT=3000 HOSTNAME=127.0.0.1
EnvironmentFile=/etc/my-app/secrets.env
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target

systemctl enable --now my-app enables and starts the service; journalctl -u my-app -f follows its logs. The app runs under a dedicated account, with no administrator rights. For Express, Fastify or NestJS, ExecStart points to the app’s entry point. With PM2, a Node.js update ends with pm2 unstartup then pm2 startup, to launch the new binary.

Nginx and HTTPS in front of the app

The Next.js documentation recommends a reverse proxy such as Nginx in front of the Node.js server: it absorbs malformed requests, slow connections, oversized uploads and traffic surges. next start listens on all interfaces by default (0.0.0.0): make it listen on 127.0.0.1, as in the unit above, so that Nginx is its only way in.

Nginx block, /etc/nginx/sites-available/my-app
server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

sudo certbot --nginx then obtains the Let's Encrypt certificate and installs it in this block; Certbot renews it on its own, and sudo certbot renew --dry-run tests the renewal. The Upgrade and Connection headers let WebSockets through.

For Next.js streaming (Suspense, loading.js), Nginx must pass responses on as they arrive: the app sends the X-Accel-Buffering: no header, declared in headers() in next.config.js, and Nginx stops buffering them.

Next.js: build, standalone and environment variables

Deployed as a Node.js server, Next.js supports all of its features: next build compiles, next start serves the app, on port 3000 by default. With output: 'standalone' in next.config.js, the build produces a self-contained .next/standalone folder, dependencies included, and a server.js to start with node, next to which two folders are copied:

Terminal, after next build
cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/

On the configuration side, three rules:

The port is set outside the .env files, since the Next.js HTTP server starts before they are read: pass it through PORT in the systemd unit. For the most sensitive secrets, the systemd documentation offers LoadCredential=: environment variables are passed on to child processes, and those set with Environment= can be read by unprivileged accounts.

PostgreSQL and the memory to plan for

At launch, PostgreSQL fits on the same server as the app: no network latency, a single server to back up. By default, it accepts local connections only (listen_addresses = 'localhost'): keep that setting, and give the app its own role, without superuser rights. The node-postgres pool opens at most 10 connections by default, and PostgreSQL typically accepts 100: add up the pools of your instances to stay under that ceiling.

What runsMemory to plan for
A Node.js process (API, Next.js)60 to 300 MB, depending on the code
Running next build on the server1 to 2 GB for a few minutes, on top of the rest
PostgreSQL25% of memory for shared_buffers, plus a few MB per connection
A Node.js API and its PostgreSQL database, first users4 GB in total
Next.js built on the server, its API and PostgreSQL6 to 8 GB in total

The Node.js heap size is capped with --max-old-space-size, in MiB: on a machine with 2 GiB, the Node.js documentation suggests 1536, to leave room for the rest of the system. Building in your continuous integration, then deploying only the result, removes the build peak.

Deploying without downtime, then staying up to date

A deployment in four steps

  1. Build alongside, in one folder per version (releases/2026-11-17): npm ci installs exactly the versions in package-lock.json, then npm run build, while the live app keeps running.
  2. Migrate the database with changes the old version supports: a column is added before it is used.
  3. Switch over: the current link points to the new folder, then the service restarts. On receiving SIGTERM, Next.js finishes the requests in progress; its documentation advises allowing it 10 to 30 seconds, hence TimeoutStopSec=30.
  4. Roll back if needed, by pointing current back to the previous folder.

To remove the restart time as well, run two instances of the same build, on ports 3000 and 3001, in an Nginx upstream block, and restart them one after the other: Nginx hands requests to whichever one answers (proxy_next_upstream error timeout, its default setting). With PM2 in cluster mode, pm2 reload chains the restarts the same way. If the app revalidates pages (ISR), the Next.js documentation describes the cache to share between instances.

Staying up to date

Your Node.js app, in production and closely monitored

With a Simafri Managed VPS, our teams keep your server up to date, monitor it day and night and step in without waiting for your call; they also proactively monitor your Node.js app and recommend what will make it faster and more secure, which you apply, yourself or through your AI assistant. Encrypted backups every night, kept off the machine, and free migration. If you would rather stay in control, VPS Pro includes Full-Stack Assistance: we diagnose and tell you what to do, from the operating system down to your application code.

Explore Managed VPS

Frequently asked questions

Which version of Node.js should I choose for production?

An LTS version. Node.js 26 has been in Active LTS since 28 October 2026, maintained until 30 April 2029; Node.js 24, in Maintenance LTS, is maintained until 30 April 2028.

PM2 or systemd for a Node.js app?

Both restart the app after an error and when the server boots. systemd is already on the system and stores logs in the system journal. PM2 adds cluster mode and zero-downtime reloads, with pm2 startup then pm2 save to survive a reboot.

How much memory does a Next.js app with PostgreSQL need?

4 GB for a Node.js API and its PostgreSQL database in their early days, 6 to 8 GB if Next.js is built on the server, since the build alone takes 1 to 2 GB for a few minutes.

How do I deploy a Node.js app without downtime?

Build the new version in its own folder while the old one runs, then switch over. To remove the restart time, run two instances behind Nginx and restart them one after the other, or use pm2 reload in cluster mode.

Contact us