# Deploying an Astro + PostgreSQL app on Railway

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

Author: Flavio Copes | Published: 2025-04-30 | Canonical: https://flaviocopes.com/deploying-an-astro-postgresql-app-on-railway/

I’m writing this to show how to deploy an app built on [Astro](https://flaviocopes.com/astro-introduction/) and SSR, which uses a PostgreSQL database for managing data, on [Railway](https://railway.app/?referralCode=kqLGRd) (referral link).

First, I assume you have a Railway account, and the app is already deployed on [GitHub](https://flaviocopes.com/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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/1.webp)

Now click the **Create** button to add a new service.

![Railway dashboard showing the Create button to add a new service to the project](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/2.webp)

Choose GitHub repo:

![Railway service creation dialog with GitHub repo option selected](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/3.webp)

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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/4.webp)

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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/5.webp)

Click “Add a variable reference”:

![Railway Add a variable reference button in the Variables tab](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/6.webp)

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

![Railway DATABASE_URL variable selection from the available environment variables](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/7.webp)

> 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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/8.webp)

> 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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/9.webp)

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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/10.webp)

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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/11.webp)

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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/12.webp)

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](https://budivoogt.com/blog/drizzle-migrations), 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:

```typescript
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](https://flaviocopes.com/images/deploying-an-astro-postgresql-app-on-railway/13.webp)

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

Before you send real users to it, I built a free [deploy checklist](https://flaviocopes.com/tools/deploy-checklist/) that generates a pre-launch list tailored to your stack and platform.
