Skip to content
FLAVIO COPES
flaviocopes.com
2026

Deploying an Astro + PostgreSQL app on Railway

By Flavio Copes

Learn how to deploy an Astro and PostgreSQL app on Railway, connecting the DATABASE_URL variable, setting a custom start command, and running migrations.

~~~

I’m writing this to show how to deploy an app built on Astro and SSR, which uses a PostgreSQL database for managing data, on Railway (referral link).

First, I assume you have a Railway account, and the app is already deployed on GitHub, in a private or public repository.

Create a new project on Railway, and deploy PostgreSQL first:

Railway new project dashboard with PostgreSQL service deployment option highlighted

Now click the Create button to add a new service.

Railway dashboard showing the Create button to add a new service to the project

Choose GitHub repo:

Railway service creation dialog with GitHub repo option selected

Select your repo (if it’s the first time using this, you’ll first have to connect GitHub):

Railway GitHub repository selection interface showing available repositories to deploy

Now in the Variables tab of this service we connect the app to the database:

Railway service Variables tab interface for connecting app to database

Click “Add a variable reference”:

Railway Add a variable reference button in the Variables tab

And you’ll find the DATABASE_URL variable, click it:

Railway DATABASE_URL variable selection from the available environment variables

Important: use the Railway internal network URL, not the public URL (DATABASE_PUBLIC_URL), which would charge you egress fees):

Now click “Add”, done!

Railway Variables tab showing POSTGRES_URL environment variable successfully added

Notice how I changed the variable to be POSTGRES_URL as the app I’m building uses that env variable.

Ok now we need Astro to run on the host 0.0.0.0 . Add a HOST variable for this:

Railway Variables tab with HOST environment variable set to 0.0.0.0 for Astro

In the “Settings” tab now go down to the Networking options and add a Railway URL to the app:

Railway Settings tab Networking section with option to add Railway URL to the app

Go down a little again and type node ./dist/server/entry.mjs in the “Custom Start Command” option in the Deploy section:

Railway Settings Deploy section with Custom Start Command field set to node ./dist/server/entry.mjs

Now click “Deploy” at the bottom of the page.

The app should be building, it’ll take a while:

Railway deployment logs showing the app building process in progress

Once that’s completed, you’ll be able to access the app!

Now the only thing we must do is run the database migrations.

Running them automatically on deploy would take some more configuration, so we’ll take the easy route and run the migrations from our local terminal.

Go into the project on your computer, copy the DATABASE_PUBLIC_URL env variable from the Railway PostgreSQL service to your local .env file under POSTGRES_URL (I use that in the Astro app) and then run:

npx drizzle-kit migrate

Now you should see the database tables in the PostgreSQL service on Railway.

Railway PostgreSQL service showing database tables after successful migration

That’s it, the app should now be working!

Tagged: Services · All topics
~~~

Related posts about services: