Dulara Malindu
Dulara Malindu

Reputation: 1637

Connection Error accessing postgres docker container

I setup a prisma project recently and here is my docker-compose.yml file

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.31
    restart: always
    ports:
      - '4030:4466'
    environment:
      TZ: ${PRISMA_DB_TIME_ZONE}
      PRISMA_CONFIG: |
        port: 4466
        # managementApiSecret: my-secret
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: ${PRISMA_DB_PASSWORD}
            migrations: true
            rawAccess: true
  postgres:
    image: postgres:10.3
    restart: always
    ports:
    - "3306:3306"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
      TZ: ${PRISMA_DB_TIME_ZONE}
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
   prisma:
   postgres:

I can open my prisma playground and it functions without any issue. but I can't create a direct connection to the postgre container with dbeaver.

dbeaver Error message

Connection reset

Why my connection to the database fails?

This photo will be helpful. enter image description here

Upvotes: 1

Views: 1783

Answers (1)

Thilak
Thilak

Reputation: 995

postgres by default listens on port 5432.

In your postgres container specification, you should expose the port 5432 not 3306.

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.31
    restart: always
    ports:
      - '4030:4466'
    environment:
      TZ: ${PRISMA_DB_TIME_ZONE}
      PRISMA_CONFIG: |
        port: 4466
        # managementApiSecret: my-secret
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: ${PRISMA_DB_PASSWORD}
            migrations: true
            rawAccess: true
  postgres:
    image: postgres:10.3
    restart: always
    ports:
    - "5432:5432"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
      TZ: ${PRISMA_DB_TIME_ZONE}
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
   prisma:
   postgres:

If 5432 port in your host is already in use and if you want to use 3306 instead , then you can do port forwarding like below:

  postgres:
    image: postgres:10.3
    restart: always
    ports:
    - "3306:5432"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
      TZ: ${PRISMA_DB_TIME_ZONE}
    volumes:
      - postgres:/var/lib/postgresql/data

UPDATE - 1

Reason why prisma can access postgres

ports section is meant only to make our services accessible in host level. But in the container level, If a port is open in a container, any other running container can access that port with the help of networks or links section.

By default, docker-compose will create a network per docker-compose.yml file and joins all the services in that file into that network.

That's the reason we could use the <service name> as the hostname and compose will resolve that name to the ip-address of the respective (in your case postgres) container.

Upvotes: 1

Related Questions