Search code examples
postgresqldockerintellij-ideawebstorm

Can't access Postgres running with docker-compose from WebStorm (JetBrains)


I have the following docker-compose.yml:

version: '3.7'

services:
  postgres-service:
    image: postgres:12.3
    env_file:
      - .env
    ports:
      - '5432:5432'
    volumes:
      - /postgres/data/

This is my .env:

POSTGRES_USER=app_postgres_user
POSTGRES_PASSWORD=foobar
POSTGRES_DB=app_database

I know postgres-service is working because I connect manually to the service and it works with following commands:

docker-compose run postgres-service bash # connect to postgres-service
psql --host=postgres-service --username=app_postgres_user --dbname=app_database

But when I try to connect from within "Webstorm > Database" I get this error:

The connection attempt failed.
java.net.UnknownHostException: postgres-service.

Screenshot: enter image description here


Solution

  • If Webstorm is running on the same host as the container, replace postgres-service with localhost.

    If it is running elsewhere, replace postgres-service with the IP address of the docker host machine where the container resides.

    I used your docker-compose and connected with DBeaver with these settings:

    enter image description here

    Your postgres container resides in a virtual network (e.g.: 172.17.0.0/16). By default there is no route from your machine to that network.

    When you use

    ports:
          - 'src:dest'
    

    ...in your docker-compose.yml file, a DNAT rule is created from your host:src to the container:dest and that's the reason of using localhost:src or the IP address of the docker host.