Avatar of Faraz PatankarFaraz Patankar

Deploy an ExpressJS app with PostgreSQL database

In this post, we will walk you through the process of building and hosting your ExpressJS backend with Railway. From deploying the barebones example to setting up a database (+ adding dummy data to it) to working with it locally, we've got you covered!


Express is a fast, unopinionated, minimalist web framework for Node.js. You can use it to build web applications and APIs. In this post, we will cover the latter. Let's start by deploying our Express starter.

Deploying the Express starter on Railway

Deploying the Express starter on Railway

Yep, that's how easy it is. Most of our starters involve one-click deployments. We take care of all the boring work so you can focus on building your core product.

In the section above, we deployed a barebones Express app to Railway. But for any real-world application, we'd want to connect our app to a database so we can store and retrieve data.

The good thing is that with Railway, provisioning a database is as simple as (if not easier than) the deployment we just did.

Provisioning a PostgreSQL database

Provisioning a PostgreSQL database

In the GIF above, we provisioned a PostgreSQL database which we can now use with our Express application. We also provide plugins for MySQL, MongoDB, and Redis.

Let's add some dummy data to our database so that we can consume it within our application. To do this, you'd normally need to SSH into your database or download a tool that allows you to interact with it.

But with Railway, we've built a neat little UI where you can interact with your database as you would with any GUI. We even have a handy function in our command palette to generate dummy data for you!

Generate dummy data

Generate dummy data

So far, we've deployed an Express app, provisioned a PostgreSQL database, and added some dummy data to it. Next, we will clone our project locally and start adding features. Here's mine if you're just following along.

Let's start by adding and configuring the node-postgres package so we can interact with our database.

yarn add pg
yarn add -D @types/pg

Next, let's set up and use the package to show some jokes to our users.

import pg from "pg";

const pool = new pg.Pool();

app.get('/jokes', async (req, res) => {
  const { rows } = await pool.query("SELECT * FROM jokes")

  res.json(rows)
})

At this point you're probably thinking "hang on a minute, we haven't even set up the database locally". Well, that's kind of the thing with Railway, you don't have to. All your environments live in the cloud. And this is where the Railway CLI comes in.

If you head on over to the Setup section on your project's dashboard, you'll see the instructions for installing the CLI and linking to your project on Railway.

Project setup instructions

Project setup instructions

Once you've linked your project, you must now prepend your commands with railway run. This allows us to run the code using your selected Railway environment and injecting all your environment variables. This way you don't have to do any fiddling with .env files at all and you can seamlessly switch environments and automagically have the respective variables available to use.

💡
You can also use railway vars to view all the environment variables for your selected environment!

Let's try running our development server and visiting http://localhost:3333/jokes to see this in action.

railway run yarn dev
Jokes from our database

Jokes from our database

We've now built our amazing jokes API and want to showcase it to the world. To be able to do that, we need to be able to deploy our code to the cloud. To do that, we can either use the CLI and run railway up to deploy our selected environment. Or, we can set up Github triggers that will automatically trigger deployments whenever we push our code to the selected branch.

We can also set up custom domains by heading over to the Deployments tab in our project dashboard. You can check out our jokes API here and feel free to reach out to us on Twitter with yours!

We hope this guide gave you an idea about what the workflow with Railway looks like. Our main goal is for you to only have to worry about building your core app while we take care of everything else.

We also have a couple of other examples using Express in our starters repository that you might be interested in and if there's something you feel should be there but isn't, we're very happy to receive PRs for the same. 🤗