When building a CRUD-based To-Do List web application using Java Spring Boot and MySQL, everything works fine locally—until you realize you want to switch databases, maybe for features, compatibility, or performance reasons.
In this post, I’ll walk you through how I migrated the backend of my To-Do List app from MySQL to PostgreSQL, without touching the frontend. This transition keeps the app fully functional while making it easier to scale and adapt to modern SQL standards.
π Stack Overview
Before the migration, the backend stack looked like this:
-
Backend: Spring Boot (Java 17, Maven)
-
Database: MySQL (via WampServer64)
-
Frontend: React.js
-
IDE: Notepad (yes, keeping it raw!)
We’ll update the backend to use PostgreSQL instead of MySQL.
π¦ Step 1: Remove MySQL Connector from pom.xml
Open your pom.xml
file and locate the MySQL dependency:
1 2 3 4 5 | <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> |
Either delete or comment this out.
✅ Step 2: Add PostgreSQL Dependency
Still in pom.xml
, add the PostgreSQL JDBC driver instead:
1 2 3 4 5 | <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.3</version> </dependency> |
This ensures that Spring Boot can connect to a PostgreSQL database using JPA.
⚙️ Step 3: Update application.properties
Head over to src/main/resources/application.properties
and update the datasource settings. Replace the old MySQL config with the following PostgreSQL configuration:
1 2 3 4 5 6 7 8 9 10 11 12 | # PostgreSQL connection spring.datasource.url=jdbc:postgresql://localhost:5432/todo_db spring.datasource.username=todo_user spring.datasource.password=yourpassword # JPA (Hibernate) Settings spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect # Server port (optional) server.port=8080 |
If you're using a different port, adjust the 5432
part of the URL accordingly.
π️ Step 4: Create the PostgreSQL Database
If PostgreSQL isn't installed on your system yet, grab it from:
π https://www.postgresql.org/download/
Then open the psql
terminal or any PostgreSQL GUI like pgAdmin, and run:
1 2 3 | CREATE DATABASE todo_db; CREATE USER todo_user WITH PASSWORD 'yourpassword'; GRANT ALL PRIVILEGES ON DATABASE todo_db TO todo_user; |
π‘ Make sure the credentials match your application.properties
.
π§ͺ Step 5: Run Your Spring Boot App
Back in your terminal, run:
mvnw spring-boot:run
If everything is set up correctly, Spring Boot will:
-
Connect to the new PostgreSQL database
-
Auto-generate your
todo
table usingddl-auto=update
-
Serve your endpoints like before
You should see Hibernate logs showing SQL being run on PostgreSQL.
✅ Backend Now Uses PostgreSQL
Your app now works with PostgreSQL instead of MySQL, with zero changes to the actual codebase (like your Todo
model or TodoRepository
).
π Final Thoughts
PostgreSQL is a great fit for modern backend development—offering JSONB support, better standards compliance, and advanced SQL features. Whether you're planning to scale up or just want to get closer to production-ready setups, switching to Postgres is a smart step.
If you’re using tools like pgAdmin or DBeaver, you’ll also find PostgreSQL easier to inspect and work with for complex queries.
No comments:
Post a Comment