Search code examples
graphqlhasura

How to create local "copy" of remote Hasura server?


I want to set up a dev environment of Hasura on my local machine, that replicates my existing production (same tables, same schema, same data).

  • What are the required steps to achieve this task?

Solution

  • I've found this process to work well.

    1. Create a clean empty local postgresql database and Hasura instance. To update an existing local database, drop it and recreate it.

    2. Dump the schema and data from your existing Hasura server (as per the answer by @protob, but with clean_output set so that manual changes to the output do not have to be made. See pgdump for details.

      curl --location --request POST 'https://example.com/v1alpha1/pg_dump' \
        --header 'Content-Type: application/json' \
        --header 'X-Hasura-Role: admin' \
        --header 'Content-Type: text/plain' \
        --header 'x-hasura-admin-secret: {SECRET}' \
        --data-raw '{ "opts": ["-O", "-x","--inserts",  "--schema", "public"], "clean_output": true}' > hasura-db.sql
      
    3. Import the schema and data locally:

      psql -h localhost -U postgres < hasura-db.sql
      
    4. The local database has all the migrations because we copied the latest schema, so just mark them as applied:

      # A simple `hasura migrate apply --skip-execution` may work too!
      for x in $(hasura migrate status | grep "Not Present" | awk '{ print $1 }'); do
        hasura migrate apply --version $x --skip-execution
      done
      
      # and confirm the updated status
      hasura migrate status
      
    5. Now finally apply the Hasura metadata using the hasura CLI:

      hasura metadata apply
      

    Enjoy your new instance!