Documentation

Documentation versions (currently viewingVaadin 23)
New Acceleration Kits: Observability Kit, SSO Kit, and Swing Kit. Read the blog post.

Connecting to PostgreSQL

Use PostgreSQL in your Vaadin application.

PostgreSQL is a free and open source relational database that’s 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.

Vaadin Start PostgreSQL configuration.

Now the generated project includes the necessary configurations to connect to PostgreSQL. Specifically, the generated project contains the following dependency inside the pom.xml file:

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

The generated project also contains 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

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.

Caution
Avoid storing sensitive information in the project’s properties file.

It’s bad practice to store database URI, username, and passwords in the application.properties file. You should consider externalizing these properties.

Now, when you start the application, you should see the following line in your IDE 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.

6C0C05FE-BB85-4DB0-AD82-8289272364B7