Migrate a Heroku app and its Postgres database to a VPS
A tested migration from Heroku to your own server: the Procfile as Docker Compose services, config vars to a file, Scheduler to supercronic, Heroku Postgres restored with pg_restore, a DNS cutover and a way back.
TL;DR
- Build one Docker (opens in a new tab) image for the app and run each Procfile line as its own Compose service (web, worker, and the release command before each deploy), with supercronic in the same image replacing Heroku (opens in a new tab) Scheduler.
- Export config vars with
heroku config --shellinto a file Compose reads, and dropDATABASE_URL: the new database gets its own address. - Move the data with
heroku pg:backups:captureand download, thenpg_restore --no-acl --no-ownerinto PostgreSQL (opens in a new tab) in Docker, after rehearsing it once. - Turn off the app's TLS requirement for the local database, lower the DNS TTL a day ahead, and keep the Heroku app until the new server has run for a while.
On this page
To migrate a Heroku app to a VPS, you build one Docker image for the app, run each line of its Procfile as a Docker Compose service next to PostgreSQL and a reverse proxy, copy the config vars into a file on the server, and replace Heroku Scheduler with a cron that runs inside the image. The data moves with Heroku's own backup commands and pg_restore, in a short maintenance window, while the Heroku app stays ready as your way back.
This tutorial is part of our guide to moving off Vercel, Heroku, Render or Railway. It moves a small Node.js (opens in a new tab) app with a web process, a worker, a release command, a scheduled job and Heroku Postgres. The Heroku CLI commands and console steps come from Heroku's Dev Center; everything else was run in Docker, as described at the end.
What moves where
| On Heroku | On your server |
|---|---|
web process in the Procfile | A Compose service behind Caddy (opens in a new tab), which handles HTTPS |
worker process | A Compose service with the same command, no published port |
release process (release phase) | docker compose run --rm web <command> before each deploy |
| Config vars | app.env, a file next to compose.yaml, readable only by the deploy user |
| Heroku Postgres | PostgreSQL in a container with its data in a named volume |
| Heroku Scheduler | supercronic, a cron for containers, as its own service |
| Automated certificates | Caddy's automatic HTTPS (Let's Encrypt, with ZeroSSL as fallback) |
heroku logs | docker compose logs |
| Review apps | See a preview of every pull request, on your own server |
Other add-ons (Redis, a search service, an email provider) either keep running where they are, reached by their URL, or become containers; decide per add-on before you start. Heroku alternatives for apps that need a database compares where each piece can live.
Before you start
You need a Linux server with Docker and Docker Compose, SSH access with a key, and a firewall that allows only SSH, HTTP and HTTPS. How to deploy a web app to a VPS covers choosing and creating one, and the Hetzner walk-through does it with a tested cloud-init file. Harden it before it holds your customers' data: see how to harden an Ubuntu 24.04 server for production.
On your computer you need the Heroku CLI, logged in to the account that owns the app.
1. Take stock of the Heroku app
List everything the platform does for the app; each item needs a new home:
cat Procfile
heroku config --shell --app your-app > heroku.env
heroku addons --app your-app
heroku ps --app your-app
heroku domains --app your-app
heroku addons:open scheduler --app your-appThe Procfile lists the processes. Ours had three:
web: node server.js worker: node worker.js release: node migrate.jsHeroku treats only
web(the one process that receives HTTP traffic) andrelease(run during the release phase, before a new release goes live) as special; any other name, such asworkerorclock, is a process that simply runs its command.heroku config --shellprints every config var asKEY=value, single-quoting values with spaces or special characters, and double-quoting values that contain a single quote or a line break. Keep the file off git (opens in a new tab) and delete it when you are done: it holds your secrets.heroku psshows the dyno formation, for exampleweb=1 worker=1. Note it: you scale the worker to zero during the cutover and may need to restore it.The Scheduler dashboard lists each job's command and interval. Copy them exactly.
heroku domainslists your custom domains and their DNS targets (<name>.herokudns.com).
2. Make the database connection work without Heroku's TLS
Heroku Postgres requires TLS 1.2 or higher, and Heroku's own Node.js example connects with ssl: { rejectUnauthorized: false }. Many Heroku apps hard-code exactly that. A PostgreSQL container on your server's private Compose network has no TLS, so the same code fails on the new server. In our test the release command stopped with:
Error: The server does not support SSL connectionsMake the setting depend on an environment variable, so the same code runs on both sides during the move:
import pg from "pg";
// Heroku Postgres requires TLS; a database on your own server's private
// network usually does not offer it. DATABASE_SSL decides.
export const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.DATABASE_SSL === "false" ? false : { rejectUnauthorized: false },
});Deploy this change to Heroku first: it changes nothing there (the variable is unset, so TLS stays on), and the code you migrate is then the code Heroku already runs. Clients in other languages have the same switch, usually an SSL mode in the connection settings.
3. Build one image for every process
One image runs the web process, the worker, the release command and the scheduled jobs, just as one slug did on Heroku. The first stage downloads supercronic and checks it against the SHA-1 checksum its release publishes:
# Supercronic replaces Heroku Scheduler: a cron made for containers.
FROM alpine:3 AS supercronic
RUN apk add --no-cache curl
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.2.51/supercronic-linux-amd64 \
SUPERCRONIC_SHA1SUM=1692066e350a88ff4d7d6474fe3c7a6c41ec5e89 \
SUPERCRONIC=supercronic-linux-amd64
RUN curl -fsSLO "$SUPERCRONIC_URL" \
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
&& chmod +x "$SUPERCRONIC" \
&& mv "$SUPERCRONIC" /usr/local/bin/supercronic
FROM node:24-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=supercronic /usr/local/bin/supercronic /usr/local/bin/supercronic
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
USER node
CMD ["node", "server.js"]The URL and checksum are for supercronic 0.2.51 on x86-64. On an Arm server, take the linux-arm64 binary and its checksum from the same release page. Add a .dockerignore with node_modules, .env, app.env, *.dump and .git, so secrets and backups never end up in the image.
Build it in CI and push it to a registry, as described in the VPS guide's GitHub Actions step, rather than on the server.
4. Turn the Procfile into Compose services
Each Procfile line becomes a service with the same command; a YAML anchor keeps the shared settings in one place:
x-app: &app
image: ghcr.io/your-name/shop:${APP_TAG:-latest}
restart: unless-stopped
env_file: app.env
environment:
DATABASE_URL: postgresql://app:${POSTGRES_PASSWORD}@db:5432/app
DATABASE_SSL: "false"
PORT: "3000"
depends_on:
db:
condition: service_healthy
services:
web:
<<: *app
command: node server.js
worker:
<<: *app
command: node worker.js
scheduler:
<<: *app
command: supercronic /app/crontab
db:
image: postgres:18
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_DB: app
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- db-data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
caddy:
image: caddy:2
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy-data:/data
- caddy-config:/config
depends_on:
- web
volumes:
db-data:
caddy-data:
caddy-config:And the Caddyfile, with a temporary hostname for now (step 8 explains why):
new.shop.example.com {
encode zstd gzip
reverse_proxy web:3000
}What to note:
Heroku set
PORTfor you. Here you set it, and Caddy forwards to the same port. Only Caddy publishes ports; the database and the app are reachable only on Compose's private network.The release phase becomes a one-off container. Before each deploy, run the release command with the new image, then start it:
docker compose run --rm web node migrate.js docker compose up -dIf the release command fails, stop there: the running version keeps serving, as on Heroku.
Workers stop cleanly. On a deploy or restart, Compose sends SIGTERM and, by default, waits 10 seconds before it kills the process. Our worker finishes its current job on SIGTERM and exited with code 0. If your jobs take longer, set
stop_grace_periodon the worker service.PostgreSQL 18 keeps its data under
/var/lib/postgresql, in a version-named folder, so the volume is mounted there and not on/var/lib/postgresql/dataas older guides show.
5. Move the config vars into a file
Take the file from step 1, remove DATABASE_URL (the new database has its own address, set in compose.yaml) and any variables that belong to add-ons you are replacing, and save the rest as app.env next to compose.yaml:
grep -v '^DATABASE_URL=' heroku.env > app.env
chmod 600 app.envCompose reads the quoting heroku config --shell writes. We checked it with values that contain spaces, &, <>, a $ and an apostrophe; inside the container each value was exactly the original:
MAIL_FROM='Shop & Co <orders@shop.example.com>'
SHOP_NAME="O'Brien's Shop"
PRICE_NOTE='Prices in $ excl. VAT'
NODE_ENV=productionOne case does not survive: in a value that contains an apostrophe, the CLI also puts a backslash before any !, and Compose keeps that backslash. Check such values by hand.
Keep the two files apart: app.env holds the app's settings and is passed to the app's containers, while .env holds only what compose.yaml itself needs, POSTGRES_PASSWORD (a new random value, for example from openssl rand -hex 24) and APP_TAG. That way the app never sees the database's own password variable.
If your app reads NEXT_PUBLIC_ or VITE_ variables, those are fixed when the image is built, so they belong in CI's build, not in app.env.
6. Replace Heroku Scheduler
Heroku Scheduler's daily times are in UTC. Write each job as a line in a crontab file in your repository:
# Was a Heroku Scheduler job: "node tasks/cleanup.js", daily at 03:00 UTC
0 3 * * * node tasks/cleanup.jsThe scheduler service runs supercronic with it. Supercronic logs each run, its output and whether it succeeded, and by default it does not start a job again while the previous run is still going. We ran it with an every-10-seconds schedule against data with ten jobs older than 30 days:
level=info msg="cleanup removed 10 finished jobs" channel=stdout job.command="node tasks/cleanup.js"
level=info msg="job succeeded" job.command="node tasks/cleanup.js"One difference: Heroku stops a Scheduler dyno when its interval runs out (a 10-minute job is ended after about 10 minutes); supercronic does not, so a long job runs to the end.
A host crontab line that runs docker compose run --rm web node tasks/cleanup.js works as well, if you prefer to keep cron outside the containers.
7. Rehearse the database move
Do this once with the app still on Heroku, to learn how long the cutover takes and that the restore works. Take a backup and download it with the commands from Heroku's import and export guide:
heroku pg:backups:capture --app your-app
heroku pg:backups:download --app your-appYou get latest.dump, a custom-format pg_dump archive. Copy it to the server, then start only the database and restore into it:
docker compose up -d --wait db
docker compose exec -T db pg_restore --verbose --no-acl --no-owner -U app -d app < latest.dumpTwo flags matter, and one is missing on purpose:
--no-acl --no-ownerskip Heroku's roles and grants, so every table belongs to yourappuser. In our test the restored tables were owned byapp.- No
--cleaninto an empty database. Heroku's guide uses--cleanin its example of a restore into a local database. Into the empty database of a new server, it tries to drop tables that do not exist: in our test it printed nine errors and exited with code 1, although the data was complete. Leave it out the first time, or add--if-exists(--clean --if-existsexited 0 for us) when you restore over an earlier rehearsal.
Then check the data and run the release command:
docker compose exec -T db psql -U app -d app -c "select count(*) from orders"
docker compose run --rm web node migrate.jsThe counts matched the source in our test (50 orders), and sequences carried over: the next order created on the new server was number 51.
Heroku's guide adds three notes. Its backups are intended for databases up to 20 GB. If the restore complains about the heroku_ext schema, run CREATE SCHEMA heroku_ext; in the new database first. And a pg_restore that reports an unsupported version in the file header is older than the dump: restore into the same or a newer PostgreSQL major version. Heroku's default for new databases is PostgreSQL 18, which is why the Compose file uses postgres:18.
8. Test the new server on a temporary hostname
Point a spare name, such as new.shop.example.com, at the server's IP address with an A record (and AAAA for IPv6), then start everything:
docker compose up -d --waitCaddy gets a certificate for the temporary name once the DNS record resolves and ports 80 and 443 are open. Click through the app, create a record, and watch the worker pick it up with docker compose logs -f worker.
Afterwards, empty the rehearsal database so the real restore starts clean: docker compose down -v removes the containers and the database volume (and Caddy's certificates, which it fetches again).
9. Lower the DNS TTL a day ahead
Resolvers may keep your current record for as long as its TTL says. If the CNAME to Heroku has a TTL of 3,600 seconds, lower it to 300 or less at least an hour before the cutover; a day ahead is the safe habit. A low TTL is also what makes rolling back quick.
Note what Heroku asked you to create: a CNAME to the <name>.herokudns.com target for subdomains, and for the root domain an ALIAS or ANAME record (or a CNAME at a provider that allows it at the root, such as Cloudflare (opens in a new tab)), because Heroku's IP addresses change. On the new server, both become plain A and AAAA records with the server's addresses.
10. Cut over
Stop writes on Heroku. Turn on maintenance mode and record the formation, then scale the worker to zero:
heroku ps --app your-app heroku maintenance:on --app your-app heroku ps:scale worker=0 --app your-appMaintenance mode serves a static page but does not stop other dynos, and Heroku warns that Scheduler jobs can still run during it. Delete or pause your Scheduler jobs in its dashboard, or they may write to the old database after the backup.
Take the final backup and download it with the same two commands as in step 7.
Restore it on the server: start the database alone (
docker compose up -d --wait db), run the samepg_restorecommand, then the release command.Switch Caddy to the real domain. Change the Caddyfile's address from
new.shop.example.comtoshop.example.com(keep both, separated by a comma, if you like).Switch DNS. Replace the CNAME (or ALIAS) to Heroku with A and AAAA records for the server. Then start the stack and recreate Caddy so it reads the new Caddyfile and requests the certificate right away:
docker compose up -d --wait docker compose up -d --force-recreate caddyStarting Caddy after the DNS change matters: when a certificate request fails, Caddy retries with growing pauses, up to a day apart.
Check from outside:
dig +short shop.example.comshows the new address, the site answers over HTTPS, and a test order reaches the worker.
Visitors whose resolvers still hold the old record see Heroku's maintenance page until the TTL runs out.
11. Roll back if you need to
As long as nothing was written on the new server, rolling back is two steps: point DNS back to the Heroku target and run heroku maintenance:off (and scale the worker back up with the formation you noted).
Once the new server has taken real writes, copy them back first. We tested this direction too: dump the new database and restore it into the Heroku stand-in over TLS, with the connection string from heroku config:get DATABASE_URL:
docker compose exec -T db pg_dump -Fc --no-acl --no-owner -U app -d app > back.dump
pg_restore --clean --if-exists --no-acl --no-owner -d "$HEROKU_DATABASE_URL" back.dumpRun the second command from a machine with PostgreSQL 18's client tools, and add ?sslmode=require to the URL if your client does not default to TLS. Heroku's CLI can also restore a backup from a URL with heroku pg:backups:restore.
12. Clean up on Heroku
Give the new server some days of real traffic, with backups running and restored once (see backups you can restore). Then remove the custom domains (heroku domains:remove), destroy the add-ons, and finally the app, keeping a last pg:backups:download file somewhere safe. Delete heroku.env and the dump files from your computer and the server.
How we tested this
On 11 November 2026, with Docker 29.9.3 and Compose 5.7.0: a PostgreSQL 18.6 container with TLS turned on stood in for Heroku Postgres, and the app ran against it with Heroku's TLS setting (both connections used TLS). We created 50 orders and let the worker process them, generated heroku.env with the quoting function from the Heroku CLI's source, dumped the database in custom format with --no-acl --no-owner, and restored it with each of the flag combinations above. The new stack (Node.js 24.22.2, node-postgres 8.23.1, supercronic 0.2.51, Caddy 2.11.9 with a local certificate in place of Let's Encrypt) served the restored data over HTTPS, continued the sequences, ran the worker and the scheduled job, and restored back into the stand-in over TLS. The Heroku CLI, the Scheduler dashboard and real DNS changes were not part of the test.
Doing this with OpsCrew
OpsCrew, the open-source deploy tool we build, does the server side of this for an app in a GitHub (opens in a new tab) repository, with your approval at each step. It reads your Procfile: every process type except web and release becomes a worker running from your app's image, and its cron jobs run with supercronic in a container of their own, in UTC (workers and cron jobs). It runs PostgreSQL next to the app with a generated password and no public port, and gives the app DATABASE_URL (databases); a release command runs before the new version takes traffic (deploys without downtime). It works on Hetzner Cloud (opens in a new tab), DigitalOcean (opens in a new tab) or a server you already have.
OpsCrew does not read from Heroku: moving the data (steps 7 and 10) and the DNS records away from Heroku stay with you. Once the app runs on OpsCrew, opscrew retarget moves it, with its data, between the places OpsCrew deploys to (moving your app).
Where to go next
If you have not picked the new home, Heroku alternatives for apps that need a database compares the hosted platforms and a server, and self-hosted PaaS compared covers Coolify (opens in a new tab), Dokploy (opens in a new tab), CapRover and Kamal, for a Heroku-like workflow on your own server. For the whole decision, including when staying on Heroku is the better choice, go back to moving off Vercel, Heroku, Render or Railway.
Key facts
| Fact | Value | Source |
|---|---|---|
| Heroku Postgres default version | PostgreSQL 18Versions 16, 17 and 18 available; 15 is deprecating, with end of life on 28 February 2027. | Heroku Postgres version support (opens in a new tab)Verified on |
| PGBackups size guidance | Intended for databases up to 20 GBLarger databases should follow Heroku's other backup guidance. | Importing and exporting Heroku Postgres databases (opens in a new tab)Verified on |
| Heroku Scheduler intervals | Every 10 minutes, every hour, or every dayExecution is expected but not guaranteed; a job may rarely be skipped or run twice. | Heroku Scheduler (opens in a new tab)Verified on |
| Heroku Postgres connections | SSL required, TLS 1.2 or higher | Connecting to Heroku Postgres (opens in a new tab)Verified on |
| Versions used in this tutorial's tests | Docker 29.9.3, Compose 5.7.0, PostgreSQL 18.6, Node.js 24.22.2, node-postgres 8.23.1, supercronic 0.2.51, Caddy 2.11.9Tested in Docker on 11 November 2026. | How we tested this (opens in a new tab)Verified on |
Frequently asked questions
How do I export my Heroku Postgres database?
Run heroku pg:backups:capture --app your-app, then heroku pg:backups:download --app your-app, to get latest.dump, a custom-format pg_dump file. Restore it into any PostgreSQL of the same or a newer major version with pg_restore --no-acl --no-owner. Heroku says its backups are intended for databases up to 20 GB; above that, follow its logical backup guidance.
How long is the downtime when moving from Heroku?
Roughly the time to capture, download and restore the database, plus the DNS TTL for visitors whose resolvers still hold the old record. For a small database that is minutes. Rehearse the move once on a copy to measure it before you announce the window.
Sources
- Importing and exporting Heroku Postgres databases (opens in a new tab)
- Heroku Postgres version support (opens in a new tab)
- Connecting to Heroku Postgres (opens in a new tab)
- The Procfile (opens in a new tab)
- Configuration and config vars (opens in a new tab)
- Heroku CLI commands (opens in a new tab)
- Heroku Scheduler (opens in a new tab)
- Maintenance mode (opens in a new tab)
- Custom domain names for apps (opens in a new tab)
- Supercronic (opens in a new tab)
- Automatic HTTPS (opens in a new tab)
- Heroku CLI source, config command and quoting (opens in a new tab)
- Compose file reference, stop_grace_period (opens in a new tab)
Related articles
- Deploying
Deploy Next.js to a VPS with Docker and HTTPS
A tested setup for Next.js 16 on your own server: standalone output, a small multi-stage Docker image, PostgreSQL and Caddy with automatic HTTPS, and the build-time traps that break apps after the first deploy.
- Deploying
Deploy a Replit app to a VPS
How to move a Replit app to a VPS you control: push it to GitHub, turn the .replit build and run commands into a Dockerfile, re-create the secrets, and move the production database with pg_dump.
- Deploying
How to deploy a web app to a VPS: the complete guide
From an empty server to a production app: server size, Docker, a reverse proxy with HTTPS, DNS, CI/CD, secrets, backups, zero-downtime updates, previews, logs and hardening.