Next.js and Postgres on Railway: a working order
Deploy Next.js with Postgres on Railway: service, database reference variable, port, migrations, the two DNS records for a domain, and failed deploys.
The whole deploy in one paragraph
On Railway a Next.js app with Postgres is one project holding two services: a web service built from your GitHub repository and a Postgres service. You connect them by setting DATABASE_URL on the web service to the reference ${{Postgres.DATABASE_URL}}, make sure the app listens on the PORT Railway injects, run migrations in the start command or a pre-deploy command, and attach a custom domain with two DNS records, a CNAME and a TXT. The order below avoids the dead ends, and it is the order Shipyard's Railway adapter runs.
Two things to settle before the first click
The first is the plan. A new account starts on a trial with a one-time 5 dollar credit that lasts 30 days, and the trial allows one custom domain. example.com and www.example.com count as two, so a product that wants both needs the Hobby plan, which allows two custom domains per service. As of November 2026 Hobby is 5 US dollars a month and includes 5 dollars of usage; anything above that is billed on top.
The second is GitHub. Railway deploys through its own GitHub App, and it only sees repositories you granted that app. If you installed it with Only select repositories, a repository you create next week is invisible to Railway until you add it. Railway also needs at least one project member whose connected GitHub account has contributor access to the repository, and the app must have no permission update waiting for your approval in GitHub.
Project, web service, database
- Create a project. A project is the container for everything one product needs, and it comes with an environment called production.
- Add a service from your GitHub repository. In the service settings choose the branch that triggers deploys, normally
main. From then on every push to that branch builds and deploys. - Add Postgres with the + New button on the project canvas, or from the command palette (Ctrl or Cmd and K). It appears as a second service named Postgres and exposes
PGHOST,PGPORT,PGUSER,PGPASSWORD,PGDATABASEand a ready-madeDATABASE_URL. - On the web service, add a variable
DATABASE_URLwhose value is the reference shown below. - Review and deploy the staged change. Railway does not apply variable edits immediately: adding, changing or removing one creates a staged change that you have to deploy.
DATABASE_URL=${{Postgres.DATABASE_URL}}Why a reference and not a pasted connection string
The syntax is ${{SERVICE_NAME.VAR}}, and Railway resolves it inside the project each time the service starts. The value on the web service is never a copy of the password, so there is nothing to update if the database credentials change, and nothing sensitive passes through whoever set it. When Shipyard sets this variable it writes only the reference; the password never reaches your computer.
The service name in the reference has to match exactly. Railway names the first database Postgres. A second one added to the same project gets a different name, and a reference to Postgres keeps pointing at the first. Check the name on the canvas before typing the reference.
The database is private by default: it is reachable from services in the same project and not from the internet. If you want to connect from your laptop, open the Postgres service, Settings, Networking, and add Public Access. That creates a TCP proxy and fills in DATABASE_PUBLIC_URL. Traffic through the proxy is billed as network egress, so keep the app itself on the private DATABASE_URL.
Port, start command and migrations
Railway injects a PORT variable and its edge proxy sends requests to that port. The app must listen on it, on host 0.0.0.0. The starter Shipyard commits uses plain next start, which picks the port up from the environment; Railway's troubleshooting page suggests passing it explicitly if you are unsure. An app that listens on a fixed port, or only on localhost, builds without error and then answers every request with a 502.
A new database is empty, so the schema has to be created on the first deploy. There are two places to do it. The simple one is the start command: run the migration script, then start the server. That is what the starter does, and it means a failed migration stops the new version from starting.
The other is a pre-deploy command, set in the service settings. It runs after the build and before the new version goes live, inside the private network and with the service's variables, so it can reach the database. If it exits with anything other than 0, the deployment does not proceed and the previous version stays up. It runs in its own container with no volumes mounted, so use it for database work only, not for writing files the app needs.
{
"scripts": {
"build": "next build",
"start": "node scripts/migrate.mjs && next start",
"start:explicit-port": "node scripts/migrate.mjs && next start --port ${PORT-3000}"
}
}The custom domain needs two records
In the web service open Settings, find Public Networking and press + Custom Domain. Railway shows a CNAME record and a TXT record. Both are required. The CNAME routes traffic; the TXT proves the domain is yours. With only the CNAME in place the domain does not verify, and requests to it return a 404 even though DNS resolves. People who followed an older guide that mentions one record get stuck exactly here.
Railway does not publish a fixed IP address, so an A record is not an option. For www that is no problem. For the bare domain it means your DNS provider must support a CNAME at the root, usually called CNAME flattening or an ALIAS record. Cloudflare does. Railway lists several providers that do not, among them AWS Route 53 and GoDaddy, and suggests moving the nameservers to Cloudflare in that case.
Once both records resolve, Railway issues a Let's Encrypt certificate by itself, usually within an hour. If the domain shows the wrong content, check its target port: each domain is mapped to one internal port, and it has to be the port the app listens on.
| Type | Name | Value | Purpose |
|---|---|---|---|
| CNAME | the domain, for example www | the target Railway shows for this domain | Routes traffic |
| TXT | the host name Railway shows | the verification value Railway shows | Proves ownership; without it the domain returns 404 |
Which of this Shipyard does
With Railway as the host, Shipyard creates the project, a service named web (and one named worker if you asked for it, started with npm run worker), deploys Postgres, connects the repository and branch, adds the bare domain and www, writes the CNAME and TXT records Railway returns into your Cloudflare zone, and waits up to 20 minutes for each certificate. It then writes every variable to each service in one call, without the replace option, so variables you added by hand survive. Its health check requests the site over https and reads /api/health to see whether the database answers.
Before that, three things are yours: adding a payment method and choosing the Hobby plan, installing the Railway GitHub App on the account that owns the repository, and creating the token. If your login has several workspaces, Shipyard needs the workspace ID, which is a long code found under the workspace's Settings, General, and not the workspace name.
Reading a deploy that did not work
A deployment moves through Initializing, Building and Deploying to Active. The state it stops in tells you which log to open.
- A failed deploy does not take the site down. The previous deployment keeps serving until a new one becomes Active.
| What you see | Likely cause | Where to look |
|---|---|---|
| Failed during Building | The build command failed: a type error, a missing dependency, a variable the build needs. | Build logs. Variables are available to the build, but only after the staged change was deployed. |
| Failed during Deploying | The pre-deploy command exited non-zero, or the new version could not be started. | Deploy logs. |
| Crashed | The app started and then exited: a required variable is missing, or the database address is wrong. | Deploy logs, first lines after start. |
| Active, but the browser shows "Application failed to respond" (502) | The app is not listening on PORT and 0.0.0.0, or the domain's target port differs from the app's port. | The start command and the domain's target port. |
| Active on the Railway address, 404 on your domain | The TXT record is missing. | Your DNS provider. |
| A push did nothing | The GitHub App cannot see the repository, or the service watches another branch. | GitHub, Settings, Applications; then the service settings. |
Driving Railway from a script
Railway has a public GraphQL API at one address. Which token you send decides what it can reach, and one of the three uses a different header.
The quickest check is the me query below, with one catch that looks like a broken token: it only works with an account token. A workspace token is not a user, so me fails for it although the token is valid. Shipyard's adapter tries me first and falls back to listing projects for that reason.
Two more things we learned writing that adapter. Managed Postgres is deployed as a template, and the call returns before the database exists: the service appears a few seconds later. A script that checks at once, finds nothing and deploys again ends up with two databases, so Shipyard polls for up to two minutes before deciding anything. And when setting variables through the API, leave the replace option off unless you mean it; with it on, every variable not in your request is removed.
curl --request POST \
--url https://backboard.railway.com/graphql/v2 \
--header 'Authorization: Bearer <API_TOKEN_GOES_HERE>' \
--header 'Content-Type: application/json' \
--data '{"query":"query { me { name email } }"}'| Token | Reaches | Created at | Header |
|---|---|---|---|
| Account | All your workspaces and resources | Account settings, Tokens, with "No workspace" selected | Authorization: Bearer |
| Workspace | One workspace | The same page, with a workspace selected | Authorization: Bearer |
| Project | One environment of one project | Project settings, Tokens | Project-Access-Token |
Sources
- Railway docs: Working with domainsdocs.railway.com
- Railway docs: Using variablesdocs.railway.com
- Railway docs: PostgreSQLdocs.railway.com
- Railway docs: Application failed to responddocs.railway.com
- Railway docs: Add a pre-deploy commanddocs.railway.com
- Railway docs: Public APIdocs.railway.com
Launch in an hour, not a weekend
Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.
See how it works