Documentation

Documentation versions (currently viewingVaadin 24)

Connecting to PostgreSQL

How to use PostgreSQL in a 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).

Add 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 do this by following the steps below:

  • In Vaadin Start, select the SETTINGS tab.

  • Scroll down until you see the Technology section.

  • 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
Don’t Store Sensitive Information in a Project’s Properties File

It’s bad practice to store database URI, username, and passwords in the application.properties file. You should instead 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 Project

For an existing Vaadin project, you can connect to PostgreSQL by adding the previously mentioned PostgreSQL dependency to the pom.xml file. Then you’d add the previously mentioned PostgreSQL configuration to the application.properties file.

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