JavaRush /Java Blog /Random EN /How to deploy Postgres in Docker and connect to a Spring-...
Павел
Level 11

How to deploy Postgres in Docker and connect to a Spring-Boot application

Published in the Random EN group
Before this you need to: - Install Docker; - Install IntelliJ IDEA Ultimate and get it free for 30 days; - Install the Docker plugin in Intellij IDEA. For example, you created a Spring-Boot application with an entity:
@Entity
public class Person {

    @Id
    @Column
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    Long id;

    @Column
    String name;

//конструктор, геттеры, сеттеры
}
To implement a connection to Postgres, perform the following steps: 1. Insert the dependency into pom.xml
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.3.1</version>
</dependency>
2. In the root folder of the project, create a file: docker-compose.yaml (there is a typo in the screenshots) The picture shows the root folder java-rush-docker How to deploy Postgres in Docker and connect it to a Spring-Boot application.  - 1 File contents:
version: '3'

services:
  postgres:
    image: 'postgres:13'
    container_name: 'java-postgres'
    ports:
    - 5432:5432
    environment:
      - 'POSTGRES_USER=sa'
      - 'POSTGRES_HOST_AUTH_METHOD=trust'
      - 'POSTGRES_DB=java'
On the same line with postgres: there will be a green arrow, you need to click it. At the bottom of IntelliJ IDEA, in the Services tab, the process will be displayed, after execution the following entry should appear: Status: Downloaded newer image for postgres:13 Creating java-postgres ... 'Compose: docker-compouse.yaml' has been deployed successfully. It means everything is OK. Something like this will appear in the left window: How to deploy Postgres in Docker and connect it to a Spring-Boot application.  - 2 This means that the java-postgres container has been launched in the postgres service . There should be a postgres 13 image in the Images folder . 3. The application.yaml (or application.properties ) file must contain the following settings:
spring:
  datasource:
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/postgres
    username: sa
    password:

  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
Now you can run the Spring-Boot project. 4. After the project has started, set up the connection: How to deploy Postgres in Docker and connect it to a Spring-Boot application.  - 3 Click the Database tab , click “+” in the drop-down list, select Postgres , enter the user name (username: sa ) in the user field , check if there is a connection. If everything is good, then click Apply and Ok . The following window will appear: The database is deployed and connected. If you want to learn more about Docker: In this article , find a video about Docker and implement what is shown there. Read the Docker basics guide. Save the Basic Docker Commands.How to deploy Postgres in Docker and connect it to a Spring-Boot application.  - 4
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION