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
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:
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.
according to this
you can either extend postgres docker image and
COPY
yourinit.sql
in it or use a bash script like this (taken from here):compose: