Skip to content
Getting Started

Use Supabase with Spring Boot

Learn how to create a Spring Boot project and connect it to your Supabase project.

AI Prompt
Help me add Supabase to my Spring Boot project. Create a Supabase project at database.new. Then: 1. Run `curl https://start.spring.io/starter.zip -d dependencies=web,data-jpa,postgresql -d type=maven-project -d language=java -d groupId=com.example -d artifactId=instruments -d name=instruments -o instruments.zip` and unzip it to scaffold the project. 2. Copy the JDBC connection string for the Session pooler (port 5432) from the Supabase Connect panel and export it as a `SUPABASE_DB_URL` environment variable, so the password stays out of source control. Set `spring.datasource.url=${SUPABASE_DB_URL}` and `spring.datasource.driver-class-name` in `application.properties`. Avoid the Transaction pooler (port 6543) since Hibernate relies on prepared statements. 3. Set `spring.jpa.hibernate.ddl-auto=update` and `spring.jpa.properties.hibernate.default_schema` in `application.properties`, so Hibernate creates tables outside the `public` schema that Supabase exposes as a data API. 4. Create an `Instrument` JPA entity mapped to the `instruments` table with `@Table(name = "instruments")`, and an `InstrumentRepository` extending `JpaRepository`. 5. Add a `CommandLineRunner` bean to `InstrumentsApplication` that seeds the table with a few instruments the first time the app starts. 6. Create an `InstrumentController` with a `GET /instruments` endpoint that returns `instrumentRepository.findAll()`. 7. Run `./mvnw spring-boot:run` and open http://localhost:8080/instruments. REFERENCE https://supabase.com/docs/guides/getting-started/quickstarts/spring-boot.md

Prerequisites#

Before you begin, make sure you have:

  • Java 17 or later, which you can check with java -version
  • curl and unzip, 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.

Save your database password securely. You need it for the connection string.

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.

1
curl 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.zip
9
unzip instruments.zip -d instruments && cd instruments

3. 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:

1
npx skills add supabase/agent-skills

Supabase 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#

  1. Navigate to your project dashboard and click on Connect.

  2. 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.

  3. Set sslmode=require either on the connection string itself or as an explicit config option if your framework sets it separately. Most drivers default 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.

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.

1
export 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.

src/main/resources/application.properties
1
spring.datasource.url=${SUPABASE_DB_URL}
2
spring.datasource.driver-class-name=org.postgresql.Driver
3
spring.jpa.hibernate.ddl-auto=update

If 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:

1
create schema if not exists app;

Then point Hibernate at the schema in application.properties.

src/main/resources/application.properties
1
spring.jpa.properties.hibernate.default_schema=app

6. 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.

src/main/java/com/example/instruments/Instrument.java
1
package com.example.instruments;
2
3
import jakarta.persistence.Entity;
4
import jakarta.persistence.GeneratedValue;
5
import jakarta.persistence.GenerationType;
6
import jakarta.persistence.Id;
7
import jakarta.persistence.Table;
8
9
@Entity
10
@Table(name = "instruments")
11
public class Instrument {
12
13
@Id
14
@GeneratedValue(strategy = GenerationType.IDENTITY)
15
private Long id;
16
17
private String name;
18
19
public Instrument() {}
20
21
public Instrument(String name) {
22
this.name = name;
23
}
24
25
public Long getId() {
26
return id;
27
}
28
29
public String getName() {
30
return name;
31
}
32
33
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.

src/main/java/com/example/instruments/InstrumentRepository.java
1
package com.example.instruments;
2
3
import org.springframework.data.jpa.repository.JpaRepository;
4
5
public 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.

src/main/java/com/example/instruments/InstrumentsApplication.java
1
package com.example.instruments;
2
3
import org.springframework.boot.CommandLineRunner;
4
import org.springframework.boot.SpringApplication;
5
import org.springframework.boot.autoconfigure.SpringBootApplication;
6
import org.springframework.context.annotation.Bean;
7
8
@SpringBootApplication
9
public class InstrumentsApplication {
10
11
public static void main(String[] args) {
12
SpringApplication.run(InstrumentsApplication.class, args);
13
}
14
15
@Bean
16
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.

src/main/java/com/example/instruments/InstrumentController.java
1
package com.example.instruments;
2
3
import java.util.List;
4
5
import org.springframework.web.bind.annotation.GetMapping;
6
import org.springframework.web.bind.annotation.RestController;
7
8
@RestController
9
public class InstrumentController {
10
11
private final InstrumentRepository instrumentRepository;
12
13
public InstrumentController(InstrumentRepository instrumentRepository) {
14
this.instrumentRepository = instrumentRepository;
15
}
16
17
@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:run

Production 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#