graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

HASURA_GRAPHQL_DATABASE_URL no effect

Open rongcuid opened this issue 3 years ago • 8 comments

Version Information

Server Version: 2.7.0

Environment

Docker compose

What is the expected behaviour?

The following docker-compose.yml should give a running instance with default database connection, but it does not.

version: '3.6'
services:
  postgres:
    image: postgres:14
    restart: always
    volumes:
    - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword
  graphql-engine:
    image: hasura/graphql-engine:v2.7.0.cli-migrations-v3
    # volumes:
    # - ./hasura/migrations:/hasura-migrations
    # - ./hasura/metadata:/hasura-metadata
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    restart: always
    environment:
      ## postgres database to store Hasura metadata
      HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
      ## 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: postgres://postgres:postgrespassword@postgres:5432/postgres 
      HASURA_GRAPHQL_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
volumes:
  db_data:

Keywords

  • HASURA_GRAPHQL_DATABASE_URL
  • PG_DATABASE_URL
  • Configuration

rongcuid avatar May 29 '22 02:05 rongcuid

Try

PG_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres

After that you need to manually track it

# Connect DB
curl -d '{
  "type": "pg_add_source",
  "args": {
    "name": "postgres",
    "configuration": {
      "connection_info": {
        "database_url": {
          "from_env": "PG_DATABASE_URL"
        },
        "pool_settings": {
          "retries": 1,
          "idle_timeout": 180,
          "max_connections": 50
        }
      }
    }
  }
}
' -H "Content-Type: application/json" \
  -H "X-Hasura-Role: admin" \
  -H "X-hasura-admin-secret:  secret" \
  -X POST http://hasura-url/v1/metadata

kaushik94 avatar May 29 '22 18:05 kaushik94

Is this second command documented anywhere? I didn't get this from the quick start documentation.

rongcuid avatar May 29 '22 21:05 rongcuid

@rongcuid i was unfortunately not able to reproduce this. starting hasura with your configuration does start hasura with a default db connected. could you add any more details on your setup that might help reproduce this.

rikinsk avatar May 31 '22 13:05 rikinsk

Unfortunately my full yml contains a private container, but I will put it down here anyways:

version: "3.6"
services:
  postgres:
    image: postgres:14
    restart: always
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword
  graphql-engine:
    image: hasura/graphql-engine:v2.7.0.cli-migrations-v3
    volumes:
      - ./hasura/migrations:/hasura-migrations
      - ./hasura/metadata:/hasura-metadata
    ports:
      - "8080:8080"
    depends_on:
      - "postgres"
    restart: always
    environment:
      ## postgres database to store Hasura metadata
      # HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
      ## 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: postgres://postgres:postgrespassword@postgres:5432/postgres
      HASURA_GRAPHQL_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
      HASURA_GRAPHQL_NO_OF_RETRIES: 10
      ## 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
  web:
    # build: .
    image: msccc
    ports:
      - "8000:8000"
    depends_on:
      - "postgres"
    # - "graphql-engine"
    # restart: on-failure
volumes:
  db_data:

When Hasura starts, I navigate to the console and go to the data tab. No database is set up automatically. I tried setting HASURA_GRAPHQL_DATABASE_URL and PG_DATABASE_URL, but neither gives the default connection. I also tested the "vanilla" image and the "migration" image, and neither sets up the connection automatically.

EDIT: metadata is set up correctly, however. It is just that there is no "Data" connection.

Right now I am working around by setting up migrations, which I need to do anyways. But I am glad to help fixing the original issue.

rongcuid avatar May 31 '22 13:05 rongcuid

Thanks for the info. Could you also confirm what metadata you are mounting at - ./hasura/metadata:/hasura-metadata. I am wondering if it is possible that an empty metadata might be getting applied as soon as the server starts which is clearing out the database connected by default. One way to test that hypothesis would be commenting out the following lines in your file and then checking if the default db is present

 #volumes:
     #- ./hasura/migrations:/hasura-migrations
     #- ./hasura/metadata:/hasura-metadata

rikinsk avatar Jun 02 '22 09:06 rikinsk

In fact, Hasura cannot start up at all if I mount an empty metadata directory. I have tried using the default (non-migration) image and without mounting the volumes. It gave the same behavior.

rongcuid avatar Jun 02 '22 12:06 rongcuid

This remains broken nearly three years later. HASURA_GRAPHQL_DATABASE_URL is referenced by the README.

https://github.com/hasura/graphql-engine/blob/0173a413bf669a4a669d2308f48cf5419f02cd2d/install-manifests/docker-compose/README.md#L20

The Compose file references PG_DATABASE_URL. https://github.com/hasura/graphql-engine/blob/0173a413bf669a4a669d2308f48cf5419f02cd2d/install-manifests/docker-compose/docker-compose.yaml#L17-L18

The docs at https://hasura.io/docs/2.0/deployment/graphql-engine-flags/reference/#database-url mention PG_DATABASE_URL, but it seems we must manually create the connection from the environment variable. Everything seems to imply that this should be automatic.

I am using hasura/graphql-engine:v2.46.0.

clintonb avatar Apr 11 '25 07:04 clintonb

Just got it working with HASURA_GRAPHQL_METADATA_DATABASE_URL. The key was to kill the metadata volume—docker volume rm hasura_db_data—which is fine since I'm just starting.

clintonb avatar Apr 11 '25 07:04 clintonb