skip to Main Content

I have a springboot application that conncects to a postgres database that i want to be dockerised. The below is my docker-compose.yml file.

version: "3.1"

services:
  db-minot-postgres:
    image: "postgres:latest"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: minot
    ports:
      - "5432:5432"
    volumes:
      - ./data:/var/lib/postgresql/data
    networks:
      - minot-network
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: minotaur:1.0
    depends_on:
      - db-minot-postgres
    environment:
      SPRING_DATASOURCE_DB: minotaur
      SPRING_DATASOURCE_URL: jdbc:postgresql://db-minotaur-postgres:5432/minot
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: postgres
      SPRING_DATASOURCE_DRIVER-CLASS-NAME: org.postgresql.Driver
    ports:
      - "8080:8080"
    networks:
      - minot-network

networks:
  minot-network:

I first pulled the postgres image and then i deploy the application below so that it connects to the postgres image but i get the error

 minot-db-minot-postgres-1  | 2023-03-27 05:55:09.829 UTC [35] FATAL:  database "minot" does not exist

I dont know why it cant detect the databse as i thought when declaring the enviroment variables since the database minotaur is declared it shouldnt be a problem. Help please. How do i stop this error.

2

Answers


  1. Looks like you have successfully connected your Postgresql inside Docker but didn’t created the database as the error clearly states your database minot does not exist.

    To create database inside docker you can follow these steps

    • Make sure that the PostgreSQL Docker container is running by running the command docker-compose up in your project directory.

    • Open a new terminal window and run the following command to enter the running PostgreSQL container:

        docker exec -it <container_name> bash
    
    • Once you’re inside the container, you can use the psql command to create the database.
       psql -U root -c 'CREATE DATABASE minot;'
    

    This will create a new database called minot.

    Exit the container by running exit.
    Restart the Spring Boot application and try accessing the database again.
    This time, the database should exist and the error should be resolved.

    Login or Signup to reply.
  2. according to this

    you can either extend postgres docker image and COPY your init.sql in it or use a bash script like this (taken from here):

    #!/bin/bash
    set -e
    
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
        CREATE USER docker;
        CREATE DATABASE my_project_development;
        GRANT ALL PRIVILEGES ON DATABASE my_project_development TO docker;
        CREATE DATABASE my_project_test;
        GRANT ALL PRIVILEGES ON DATABASE my_project_test TO docker;
    EOSQL
    

    compose:

      db-minot-postgres:
        image: "postgres:latest"
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: minot
        ports:
          - "5432:5432"
        volumes:
          - ./data:/var/lib/postgresql/data
          - ./init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
        networks:
          - minot-network
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search