Documentation

Documentation versions (currently viewingVaadin 23)

Connecting to PostgreSQL

PostgreSQL is a free and open source relational database that is commonly used to persist data for Spring backends. You should install PostgreSQL separately before you can connect to it from your Vaadin application (you can also install PostgreSQL as a Docker container).

Adding PostgreSQL to New Vaadin Applications

The easiest way to use PostgreSQL in your Vaadin project is to include it when generating a starter project from Vaadin Start. You can achieve this by following the steps below:

  1. In Vaadin Start, select the SETTINGS tab.

  2. Scroll down until you see the Technology section.

  3. Select PostgreSQL as the Database.

Now the generated project will include the necessary configurations to connect to PostgreSQL. Specifically, the generated project will contain the following dependency inside the pom.xml file:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
</dependency>

The generated project will also contain the following PostgreSQL configuration added to the src/main/resources/application.properties file:

# PostgreSQL configuration.
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.username = postgres
spring.datasource.password = password
spring.jpa.hibernate.ddl-auto = update
Tip
The generated PostgreSQL configuration assumes common defaults for the host, port, username, and password. If your PostgreSQL instance was set up differently, then you should match the properties in the application.properties file to the appropriate properties from your database instance.

Now, when you start the application, you should see the following line in your IDE’s console.

Database available at 'jdbc:postgresql://localhost:5432/postgres'

Adding PostgreSQL to an Existing Vaadin Project

For an existing Vaadin project, you can connect to PostgreSQL by:

  1. Adding the previously mentioned PostgreSQL dependency to the pom.xml file.

  2. Adding the previously mentioned PostgreSQL configuration to the application.properties file.