docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.16k stars 1.13k forks source link

Spring application can't connect to postgred in docker #1022

Closed notkksm closed 1 year ago

notkksm commented 1 year ago

When i try to connect my spring application with postgres in docker i have an error FATAL: database "animals" does not exist ` I read that the reason could be that i have postgres on my pc and i changed the port, but i still see the same error. What is my mistake? Thanks! ( i see it in console:

listening on IPv4 address "0.0.0.0", port 5433
db     |  LOG:  listening on IPv6 address "::", port 5433
db     | LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433), 

docker-compose.yml

[version: '2'

services:
  app:
    image: 'docker-spring-boot-postgres:latest'
    build:
      context: .
    container_name: app
    depends_on:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5433/animals
      - SPRING_DATASOURCE_USERNAME=postgres
      - SPRING_DATASOURCE_PASSWORD=darsmeved
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update

  db:
    image: 'postgres:13.1-alpine'
    container_name: db
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=darsmeved
    expose:
      - "5433"
    ports:
      - "5433:5432"
    command: -p 5433](url)
tianon commented 1 year ago

Unfortunately, you're going to have more luck finding a dedicated support forum, such as the Docker Community Slack, Server Fault, Unix & Linux, or Stack Overflow (we don't have the bandwidth to provide detailed support in our issue tracker).

yosifkit commented 1 year ago

If you are changing the port that postgres is listening on, then you need to also change the port mapping if you want to access it through the host IP.

ports:
-    - "5433:5432"
+    - "5433:5433"
command: -p 5433

Or you can just leave postgres on the default port and map to a custom port on the Docker host side. Other containers could then use the default, but access through your host IP would be the custom port.

ports:
    - "5433:5432"
-command: -p 5433