Search code examples
dockerdocker-composedocker-network

Two databases for Hasura in Docker Compose


I tried to separate the databases for hasura in Docker Compose. Idea is to have a database for the metadata of hasura. And one for my actual data. This is my docker-compose.yml file.

version: "3.8"
services:
  meta:
    image: postgres
    hostname: meta
    container_name: meta
    environment:
      POSTGRES_DB: meta
      POSTGRES_USER: meta
      POSTGRES_PASSWORD: metapass
    restart: always
    volumes:
      - db_meta:/var/lib/postgresql/data
    networks:
      - backend
  data:
    image: postgres
    hostname: data
    container_name: data
    environment: 
      POSTGRES_DB: data
      POSTGRES_USER: data
      POSTGRES_PASSWORD: datapass
    restart: always
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - backend
  graphql-engine:
    image: hasura/graphql-engine:v2.13.0
    ports:
    - "8080:8080"
    depends_on:
    - "meta"
    - "data"
    restart: always
    environment:
      ## postgres database to store Hasura metadata
      # Database URL postgresql://username:password@hostname:5432/database
      HASURA_GRAPHQL_METADATA_DATABASE_URL: meta://meta:metapass@meta:5432/meta
      ## this env var can be used to add the above postgres database to Hasura as a data source. this can be removed/updated based on your needs
      PG_DATABASE_URL: data://data:datapass@data:5432/data
      ## 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 run console offline (i.e load console assets from server instead of CDN)
      # HASURA_GRAPHQL_CONSOLE_ASSETS_DIR: /srv/console-assets
      ## uncomment next line to set an admin secret
      # HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
    networks:
      - backend
volumes:
  db_data:
  db_meta:
networks: 
  backend: 
    driver: bridge

I get {"detail":{"info":{"code":"postgres-error","error":"connection error","internal":"missing \"=\" after \"meta://meta:metapass@meta:5432/meta\" in connection info string\n","path":"$"},"kind":"catalog_migrate"},"level":"error","timestamp":"2022-10-24T14:16:06.432+0000","type":"startup"}

I think the problem is related to the hostname. But I do not know how to solve it. Any ideas?


Solution

  • I have tried a lot so now my solution looks like this. I have noticed that volumes delete makes it more easy to develop. The beginning of the Database URL must start with postgresql://. Just as @jlandercy has already said.

    version: "3.8"
    services:
    
      meta:
        image: postgres
        container_name: meta
        restart: always
        volumes:
          - db_meta:/var/lib/postgresql/data
        environment:
          POSTGRES_USER: meta_user
          POSTGRES_PASSWORD: meta_password
          POSTGRES_DB: meta_db
    
      data:
        image: postgres
        container_name: data
        restart: always
        volumes:
          - db_data:/var/lib/postgresql/data
        environment: 
          POSTGRES_USER: data_user
          POSTGRES_PASSWORD: data_password
          POSTGRES_DB: data_db
        ports:
        - 5432:5432
    
      graphql-engine:
        image: hasura/graphql-engine
        depends_on:
        - "meta"
        - "data"
        restart: always
        environment:
          ## postgres database to store Hasura metadata
          # Database URL postgresql://username:password@hostname:5432/database
          HASURA_GRAPHQL_METADATA_DATABASE_URL: postgresql://meta_user:meta_password@meta:5432/meta_db
          ## this env var can be used to add the above postgres database to Hasura as a data source. this can be removed/updated based on your needs
          PG_DATABASE_URL: postgresql://data_user:data_password@data:5432/data_db
          ## 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 run console offline (i.e load console assets from server instead of CDN)
          # HASURA_GRAPHQL_CONSOLE_ASSETS_DIR: /srv/console-assets
          ## uncomment next line to set an admin secret
          # HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
        ports:
        - "8080:8080"
    
    volumes:
      db_data:
      db_meta: