Friday, July 11, 2025

Deploying My Spring Boot ToDo List Backend to Railway — A Complete Guide

 As part of my journey in building full-stack Java applications, I recently deployed the backend of my ToDo List application — built with Spring Boot — to Railway, a modern cloud platform that makes app hosting surprisingly easy.

In this post, I’ll walk through the exact steps I took to deploy my Spring Boot backend to Railway, connect it to a PostgreSQL database, and expose a working REST API live on the web. Refer to my previous post "Uploading the Full-Stack To-Do List App to GitHub Using a Personal Access Token" as a prerequisite of the deployment process.


🛠 Tech Stack

  • Backend: Spring Boot 3.5.3

  • Database: PostgreSQL

  • Cloud Platform: Railway

  • Build Tool: Maven

  • Java Version: 17

  • API Endpoint: GET /api/todos


✅ Step 1: Prepare the Spring Boot App for Deployment

Modify application.properties, used environment variables for portability:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# PostgreSQL settings using environment variables
spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${PGUSER}
spring.datasource.password=${PGPASSWORD}

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

# Dynamic port for Railway
server.port=${PORT:8080}

⚠️ Note: PORT is dynamically set by Railway at runtime.

Push the changes to github using the following commands:

 git add .

 git commit -m "Updated props file for Railway"

 git push origin main

🗄️ Step 2 Create a Railway Project

  1. Go to Railway.app

  2. Create a New Project > Select Deploy from GitHub

  3. Connect it to the GitHub repo containing the Spring Boot backend

  4. Railway auto-detects it's a Maven project and starts building

  5. Set your build settings

  6. Set custom domain(this is important as it will be used by the frrontend as part of the the url for the api calls)

  7. Set up environment variables
Go to “Variables” tab in Railway and add any necessary environment variables like:

SPRING_DATASOURCE_URL=

SPRING_DATASOURCE_USERNAME=

SPRING_DATASOURCE_PASSWORD=

Set the respective values of each variables after deploying the PosGre Database 

 

Build Settings:

1. Root directory:
Set to backend (since your Maven project is inside /backend)
2. Build command:
./mvnw clean package -DskipTests
3. Start command:
java -jar target/*.jar

🐘 Step 3: Add PostgreSQL Database Plugin

From the Railway dashboard:

  • Click “New” → “Add Plugin” → PostgreSQL

  • Wait for the database to be provisioned

  • Open the Data tab and press the Connection button, a popup window appears and switch to Public Connection

  • Copy the connection string (e.g. postgresql://...)


🔐 Step 4: Modify Environment Variables

Add the following values to the variables created in step 2:

VariableValue
SPRING_DATASOURCE_URL
jdbc:postgresql://<host>:<port>/<db>
SPRING_DATASOURCE_USERNAME
postgres
SPRING_DATASOURCE_PASSWORD
your_db_password

Replace <host> and <port> with actual values from the public connection string.

Tip: let chatgpt identify the port, username and password by pasting the public connection string.

 And now all is set for deployment. If the deployment is successful, just like in my case, you may now test the api call by like in my case, I used the following url:

https://todo-production-40cc.up.railway.app/api/todos


Here is the result(my todo table is still empty during testing):

If it is not empty, it will return the contents of the table in json format.

 Troubleshooting Guide: You will need a lot of help from chatgpt just like in my case, just copy the logs generated and paste it chatgpt prompt and let it analyze for you.

No comments:

Post a Comment

Deploying a React Frontend to Railway: A Complete Guide (To-Do App)

 If you've already deployed your Java Spring Boot backend to Railway, congratulations — the hardest part is done! 🎉 Now it’s time to ...