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
-
Go to Railway.app
-
Create a New Project > Select Deploy from GitHub
-
Connect it to the GitHub repo containing the Spring Boot backend
-
Railway auto-detects it's a Maven project and starts building
Set your build settings
Set custom domain(this is important as it will be used by the frrontend as part of the the url for the api calls)
- 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
backend
(since your Maven project is inside /backend
)./mvnw clean package -DskipTests
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:
Variable | Value |
---|---|
|
|
|
|
|
|
<host>
and <port>
with actual values from 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