Use Supabase with Spring Boot
Learn how to create a Spring Boot project and connect it to your Supabase project.
Prerequisites#
Before you begin, make sure you have:
- Java 17 or later, which you can check with
java -version curlandunzip, to download and extract the generated project
1. Create a Supabase project#
To start, you need a Supabase project.
Create a new Supabase project from the Dashboard of any organization you belong to.
Want to create a project programmatically?
Use the Management API or ask the MCP server to create a new Supabase project.
Save your database password securely. You need it for the connection string.
This guide uses Spring Boot's own JPA entities and generated schema, not the shared instruments sample table used by other quickstarts.
2. Create a Spring Boot project#
Use Spring Initializr to scaffold a new project with the Web, Spring Data JPA, and Postgres Driver dependencies. Run the following from the directory where you keep your projects.
1curl https://start.spring.io/starter.zip \2 -d dependencies=web,data-jpa,postgresql \3 -d type=maven-project \4 -d language=java \5 -d groupId=com.example \6 -d artifactId=instruments \7 -d name=instruments \8 -o instruments.zip9unzip instruments.zip -d instruments && cd instruments3. Set up AI tooling (optional)#
Supabase provides two ways to give AI tools context about your project: Agent Skills, which give your AI coding agent procedural knowledge, and the MCP server, which connects AI assistants to your Supabase project directly.
Agent Skills#
Agent Skills is a curated set of instructions that give your AI agent procedural knowledge about working with Supabase.
Install them so your AI coding agent can produce more accurate, reliable code using current Supabase patterns, such as authentication, server-side rendering, and database migrations, rather than relying solely on training data.
Installing Agent Skills#
To install, run the following command in the root of your project:
1npx skills add supabase/agent-skillsSupabase MCP server#
The Supabase MCP server connects AI assistants to Supabase, so they can inspect your schema and act on your projects on your behalf. Find out how to add it to your client in the MCP docs.
4. Set up the Postgres connection details#
-
Navigate to your project dashboard and click on Connect.
Don't use the Transaction pooler (port
6543) as your app's main data source. Most ORMs rely on server-side prepared statements, which the Transaction pooler doesn't support. Use the Session pooler (port5432), or the direct connection string if you're in an IPv6 environment or have the IPv4 Add-On. -
Look for the Session pooler connection string and copy it. Replace the password placeholder with your saved database password, and percent-encode any reserved characters it contains, such as
&,#,?, or a space. If you don't have your database password, you can reset it in your Database Settings. -
Set
sslmode=requireeither on the connection string itself or as an explicit config option if your framework sets it separately. Most drivers default toprefer, which falls back to sending your data in plaintext if the encrypted attempt fails. You can also enforce SSL on the database side.
The connection strings below show the format only. Take the host, port, and username from the string you copied rather than typing the bracketed placeholders literally.
Select the JDBC tab to copy the connection string in the right format for Spring Boot.
The connection string contains your database password, and application.properties is committed with your project. Set the string as an environment variable instead, and set it the same way on whatever platform you deploy to.
1export SUPABASE_DB_URL='jdbc:postgresql://[POOLER-HOST]:5432/postgres?user=postgres.[PROJECT-REF]&password=[YOUR-PASSWORD]&sslmode=require'Then reference the variable, along with the driver, in src/main/resources/application.properties.
1spring.datasource.url=${SUPABASE_DB_URL}2spring.datasource.driver-class-name=org.postgresql.Driver3spring.jpa.hibernate.ddl-auto=updateIf the app fails to start with Unable to determine Dialect without JDBC metadata, Hibernate couldn't open a connection at all. Look above that line in the logs for the real cause, most commonly password authentication failed.
5. Change the default schema#
By default Hibernate creates tables in the public schema. We recommend changing this as Supabase exposes the public schema as a data API.
Create the app schema before you start the app. Hibernate creates tables in that schema on startup, but it does not create the schema itself. Run the following in the SQL Editor:
1create schema if not exists app;Then point Hibernate at the schema in application.properties.
1spring.jpa.properties.hibernate.default_schema=app6. Create an entity and repository#
Spring Data JPA maps Java classes to database tables. Create an Instrument entity in src/main/java/com/example/instruments/Instrument.java. With spring.jpa.hibernate.ddl-auto=update set, Hibernate creates the instruments table for you when the app starts.
1package com.example.instruments;23import jakarta.persistence.Entity;4import jakarta.persistence.GeneratedValue;5import jakarta.persistence.GenerationType;6import jakarta.persistence.Id;7import jakarta.persistence.Table;89@Entity10@Table(name = "instruments")11public class Instrument {1213 @Id14 @GeneratedValue(strategy = GenerationType.IDENTITY)15 private Long id;1617 private String name;1819 public Instrument() {}2021 public Instrument(String name) {22 this.name = name;23 }2425 public Long getId() {26 return id;27 }2829 public String getName() {30 return name;31 }3233 public void setName(String name) {34 this.name = name;35 }36}Create an InstrumentRepository interface in the same package. Extending JpaRepository gives you findAll, save, and other query methods without writing any implementation.
1package com.example.instruments;23import org.springframework.data.jpa.repository.JpaRepository;45public interface InstrumentRepository extends JpaRepository<Instrument, Long> {}7. Seed sample data#
Add a CommandLineRunner bean to InstrumentsApplication.java that saves some sample instruments the first time the app starts.
1package com.example.instruments;23import org.springframework.boot.CommandLineRunner;4import org.springframework.boot.SpringApplication;5import org.springframework.boot.autoconfigure.SpringBootApplication;6import org.springframework.context.annotation.Bean;78@SpringBootApplication9public class InstrumentsApplication {1011 public static void main(String[] args) {12 SpringApplication.run(InstrumentsApplication.class, args);13 }1415 @Bean16 CommandLineRunner seedInstruments(InstrumentRepository instrumentRepository) {17 return args -> {18 if (instrumentRepository.count() == 0) {19 instrumentRepository.save(new Instrument("violin"));20 instrumentRepository.save(new Instrument("viola"));21 instrumentRepository.save(new Instrument("cello"));22 }23 };24 }25}8. Query data from the app#
Create an InstrumentController that fetches every row from the instruments table through the repository and returns it as JSON.
1package com.example.instruments;23import java.util.List;45import org.springframework.web.bind.annotation.GetMapping;6import org.springframework.web.bind.annotation.RestController;78@RestController9public class InstrumentController {1011 private final InstrumentRepository instrumentRepository;1213 public InstrumentController(InstrumentRepository instrumentRepository) {14 this.instrumentRepository = instrumentRepository;15 }1617 @GetMapping("/instruments")18 public List<Instrument> getInstruments() {19 return instrumentRepository.findAll();20 }21}9. Start the app#
Run the Spring Boot app, and go to http://localhost:8080/instruments in your browser. You should see the list of instruments.
1./mvnw spring-boot:runProduction requirements#
The quickstart procedure in this guide optimizes for getting you to a working app, not for production.
Before you deploy:
- If your app reads or writes through the Data API, review your Row Level Security policies. Any policy you added here is scoped to this quickstart's sample data, not to real user data.
- Set your Supabase credentials as environment variables on whatever platform you deploy to, rather than committing them to source control.
- Configure a custom domain for your Supabase project once you're ready to go live.
Next steps#
- Set up Auth for your app
- Insert more data into your database
- Upload and serve static files using Storage
- Replace
ddl-autowith database migrations before going to production