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

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

1
npx skills add supabase/agent-skills

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

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.

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

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.

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.

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

5. 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> {}

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

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.

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
}

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

Next steps#