How to deploy PostgreSQL database to Railway
Hannah Liao

Hannah Liao @hannliao

About: Self-taught software developer. Used to be a quality engineer.

Joined:
Oct 30, 2024

How to deploy PostgreSQL database to Railway

Publish Date: Oct 30 '24
11 4

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!

Comments 4 total

  • RitaVerissimo86
    RitaVerissimo86Jan 8, 2025

    Hi,
    Thanks for this post :)
    I'm having a 404 error message at signup form submission.
    Failed to load resource: the server responded with a status of 404 ()
    What should I do to fix this?
    Thanks,
    Rita

    • Hannah Liao
      Hannah LiaoJan 8, 2025

      Hi Rita!
      Make sure you're using production variables and double check that you have a route to handle form submission (e.g., POST /signup). If all else fails, I would ask ChatGPT!

  • Amcool
    AmcoolMay 15, 2025

    Hi am using django and postgresql on windows help me setup

  • yq
    yqJul 29, 2025

    Thank you for sharing. Is it impossible to connect to the database using a private network?

Add comment