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 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 instruments2. Install Supabase's Agent Skills (optional)#
Supabase's 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.
To install, run the following command in the root of your project:
1npx skills add supabase/agent-skills3. Set up the Postgres connection details#
Go to database.new and create a new Supabase project. Save your database password securely.
When your project is up and running, navigate to its dashboard and click on Connect.
The Transaction pooler (port 6543) doesn't work as your app's main data source, because Spring Data JPA uses Hibernate, which relies on server-side prepared statements. Use the Session pooler, or the direct connection string if you're in an IPv6 environment or have the IPv4 Add-On.
Under the Session pooler (port 5432), select the JDBC tab and copy the connection string. Replace the password placeholder with your saved database password, and percent-encode any reserved characters it contains, such as &, #, ?, or a space.
You can reset your database password in your Database Settings if you do not have it.
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://xxxx.pooler.supabase.com:5432/postgres?user=postgres.xxxx&password=[YOUR-PASSWORD]&sslmode=require'The string you copied doesn't set sslmode, so add it. The driver defaults to prefer, which falls back to sending your data in plaintext if the encrypted attempt fails. You can also enforce SSL on the database side.
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.
4. 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 schema from the Table Editor as your app will need it before start. Then point Hibernate at it in application.properties.
1spring.jpa.properties.hibernate.default_schema=app5. 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> {}6. 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}7. 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}8. 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:runNext steps#
- Set up Auth for your app
- Replace
ddl-autowith database migrations before going to production - Insert more data into your database
- Upload and serve static files using Storage