sandeep
sandeep

Reputation: 3345

connect docker postgres from outside (DBeaver)

I am using Airflow through Docker-compose. The same docker-compose.yml has an image of Postgres as well. The config looks like below -

  postgres:
    image: postgres:13
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: airflow
      POSTGRES_PASSWORD: airflow
      POSTGRES_DB: airflow
    volumes:
      - postgres-db-volume:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "airflow"]
      interval: 5s
      retries: 5
    restart: always

This works fine for Airflow and I am also able to access the DAG on the UI.

But I want to access the dockerized Postgres instance from an outside SQL client application like Dbeaver. But I am not able to do that.

IP Address

Postgres Setting

Can someone please help me to resolve this?

Note: I already have a separate Postgres instance running on my local.

Upvotes: 19

Views: 26180

Answers (4)

s3c
s3c

Reputation: 1851

For me the solution was a bit different. I didn't have to change the port, but the host, because of how docker networking works (for me at least).

I checked my docker-compose.yml to see my database port mapping is to 5432 externally:

  ports:
    5432:5432
#   ^^^^
#   this one

I'm on Linux (Ubuntu 24.04) so the command to get the host IP is sudo ss -tuln | grep {port} so in my example:

$ sudo ss -tuln | grep 5432
tcp    LISTEN 0    4096    10.110.250.1:5432    0.0.0.0:*
#                          ^^^^^^^^^^^^
#                          the ip

So when I changed the host from localhost to 10.110.250.1 in DBeaver it connected successfully.

Upvotes: 0

Susobhan Das
Susobhan Das

Reputation: 1144

For windows, in docker-compose.yml the -ports: should be present.

For Linux, you can find the port number using docker inspect <image-id>, however for windows and dbeaver, the localhost is what is working.

Minimal docker-compose.yml

version: '3.9'
services:
  db:
    image: postgres:latest
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - db:/var/lib/postgresql/data
    ports:
      - "54320:5432"
volumes:
  db:
    driver: local

Also, the dbeaver connection - profile :

dbeaver connection

Upvotes: 3

ansuf
ansuf

Reputation: 341

You should put 'localhost' instead of '172.25.0.2' and ensure there is no port '5432' already used in your system by using lsof -i:5432 | grep 'LISTEN'.

I prefer to use another port. see screenshot below

enter image description here

Upvotes: 16

Prof.Plague
Prof.Plague

Reputation: 737

After trying several times, as @Jashwant mentioned, this port(5432) was occupied. Note that it is possible that this port not be shown in lsof -i -P command.

So I used another port and instead of 5432:5432, I used 6543:5432 and it solved the problem.

Upvotes: 7

Related Questions