connect hasura to existing postgresql

Viewed 946
  • Docker desktop (windows10) running in WSL2
  • postgresql running in WSL2
  • pgadmin running in windows10

I can connect with pgadmin (local machine) to postgresql (localmachine WSL2) with the default settings (localhost:5432)

postgres.conf

listen_addresses = '*'
port = 5432

When I create a docker container it will not connect to my local postgresql.

cmd used in WSL2

docker run -d --net=host \
       -e HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:password@localhost:5432/mydb \
       -e HASURA_GRAPHQL_ENABLE_CONSOLE=true \
       -e HASURA_GRAPHQL_DEV_MODE=true \
       hasura/graphql-engine:v1.3.3

error

"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}

What am I missing?

2 Answers

turned out I had to use this:

docker run -d -p 8080:8080 
-e HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:password@host.docker.internal:5432/mydb \      
-e HASURA_GRAPHQL_ENABLE_CONSOLE=true  \
-e HASURA_GRAPHQL_DEV_MODE=true        \
hasura/graphql-engine:v1.3.3

I thought "host.docker.internal" was only ment for Mac. Seems to work with Docker Desktop Windows10(WSL2) too.

here is a working solution for me, important is hostname

version: "3.8"

services: 

postgres:
    restart: always
    image: postgres
    container_name: postgres
    hostname: postgres
    #depends_on:
        #sql-server:
            #condition: service_healthy
    volumes:
        - pg_data:/var/lib/postgresql/data
    environment:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgrespassword

    networks:
        - backend

sql-api:
    restart: always
    container_name: api
    image: hasura/graphql-engine:v2.2.0
    ports:
        - 8055:8080
    depends_on:
        - "postgres"
    hostname: sqlapi
    environment:
        ## postgres database to store Hasura metadata
        HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
        ## enable the console served by server
        HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
        ## enable debugging mode. It is recommended to disable this in production
        HASURA_GRAPHQL_DEV_MODE: "true"
        HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
        ## uncomment next line to set an admin secret
        # HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey

    networks:
        - backend

networks: backend: driver: bridge

volumes: pg_data:

Related