How do I start the Hasura cluster locally?

Context

The local Hasura cluster runs two services, a postgres database and the hasura server. Every time the cluster is started, it starts from zero, brand new. Part of the initialization is to run migrations, which will populate the empty hasura database with the structure it needs as well as the hasura metadata and seed data.

Make sure you have installed the Hasura CLI. Here are instructions from the Hasura docs.

Hasura-Cluster Helper Tool

There is a tool we've created that has a few shortcuts available, it's called hasura-cluster. This just a bash script that runs a few docker-compose commands.

Syntax:

$ ./hasura-cluster <command> <params>

[1] Set your environment:

$ ./hasura-cluster setenv local

[2] Start the cluster:

$ ./hasura-cluster start

[3] Start the hasura console (press Ctrl+C to stop)

$ ./hasura-cluster console
# - or -
$ hasura console

[4] Stop the cluster (whenever you are finished):

$ ./hasura-cluster stop

Hasura-Cluster Reference

  • run_migration: Runs all 3 hasura migrations: database, metadata, seed data.

  • start_only: Starts the cluster and does NOT run migrations.

  • start: Starts the cluster and runs all migrations.

  • restart: Stops the running cluster, and starts cluster, then applies all migrations

  • stop: Stops the cluster, removes any volumes left out.

  • prune: Deletes any volumes left out by the cluster.

  • follow: Shows the logs for the running cluster.

  • status: Displays the current status of the cluster

  • setenv: Changes the current environment.

  • console: It's an alias for hasura console

Hasura-Cluster SetEnv:

What setenv really does is to copy an existing file from the ./config folder into the current folder and renames it into config.yaml. The hasura command (CLI) will read its settings from config.yaml.

For example, there should only be a file in the ./config folder called hasura.local.yaml which contains the connection information for the local cluster. Notice the format hasura.<environment>.yaml. If for example, you need to connect the Hasura Staging cluster, you would need to create a new file called hasura.staging.yaml, provide all the info, then run $ hasura-cluster setenv staging and hasura console.

AWS Cognito Integration (or lack thereof)

At the moment, the local environment does not really need to integrate with AWS cognito. You can still pass an admin key or a JWT token to the local instance, but hasura will just ignore them.

Docker

The local hasura cluster runs on top of docker. The docker-compose file looks like this, where we define a postgres 12 database saving data to a volume and port 5432, and a hasura node running on port 8080:

    moped-pgsql:
        image: postgres:12-alpine
        volumes:
            - moped-db-vol:/var/lib/postgresql/data
        expose:
            - 5432
        ports:
            - 5432:5432
        environment:
            - POSTGRES_USER=moped
            - POSTGRES_PASSWORD=moped
            - POSTGRES_DB=moped

    hasura:
        image: hasura/graphql-engine:v1.3.2
        restart: always
        depends_on:
            - moped-pgsql
        expose:
            - 8080
        ports:
            - 8080:8080
        environment:
            - HASURA_GRAPHQL_ENABLE_CONSOLE=false
            - HASURA_GRAPHQL_ENABLE_TELEMETRY=false
            - HASURA_GRAPHQL_DATABASE_URL=postgres://moped:moped@moped-pgsql:5432/moped

The above file is incomplete, you can see the full file here: <file url>

Hasura Cluster Tool

I've created a script that provides docker shortcuts to start the cluster, stop the cluster, clean up, etc.

$ ./hasura-cluster start        # Start cluster with migrations
$ ./hasura-cluster start_only   # Start without migrations
$ ./hasura-cluster stop         # Stops cluster, removes database data (data).
$ ./hasura-cluster prune        # Removes any database volumes (data)

Docker-compose

You can still run docker compose, this command will start the cluster without migrations:

$ docker-compose -f docker-compose.yaml up -d --volume

The command above runs the cluster in detached mode, the --volume flag means it will remove any volumes running, in our case it will remove the postgresql volume, and with it any stored data. After running this command, you can now manually run the hasura migrations.

Last updated