How to deploy PostgreSQL database to Railway

hannliao

Hannah Liao

Posted on October 30, 2024

How to deploy PostgreSQL database to Railway

You've finished building a Node.js app and pushed it to GitHub, but how do you host it on Railway?

  1. In app.js of your project, add '0.0.0.0' to the app.listen function as shown below. Railway requires your app to explicitly listen on all network interfaces (0.0.0.0) instead of just the default localhost. Push this change to GitHub.

    Image description

  2. From the Railway dashboard, select New and Deploy from GitHub repo. Select your project, click Deploy Now, and wait for deployment to complete.

    deploy-project

  3. Close the side window and click on the Create button. Select Database and then Add PostgreSQL. Wait for deployment to complete.

    Image description

  4. With the project card still selected, navigate to the Variables tab. Click on New Variable and add the Railway-provided DATABASE_URL. You should be able to start typing it in VARIABLE_NAME and select it from the dropdown menu. Railway will populate the value for you, then click Add.

    Image description

  5. Click on the Postgres card on the left and navigate to the Variables tab. You should see all the Railway-provided Postgres variables. Copy the DATABASE_PUBLIC_URL (if you want your app to be public).

    Image description

  6. In your terminal, change directory to your project. Run the following commands:

    export DATABASE_URL=<DATABASE_PUBLIC_URL>
    

    Run your script to populate the database.

    node db/populatedb.js <DATABASE_PUBLIC_URL>
    
  7. Great! You should see your table(s) in the Postgres Data tab.

    Image description

  8. Go to project Settings and scroll down to Networking. Under Public Networking, click on Generate Domain.

    Image description

  9. That's it! Click on the public domain, and you should be able to see your project working.

Hope this tutorial helps!

💖 💪 🙅 🚩
hannliao
Hannah Liao

Posted on October 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related