# Running SubQuery Locally

This guide works through how to run a local SubQuery node on your infrastructure, which includes both the indexer and query service. Don't want to worry about running your own SubQuery infrastructure? SubQuery provides a managed hosted service (opens new window) to the community for free. Follow our publishing guide to see how you can upload your project to SubQuery Projects (opens new window).

# Using Docker

An alternative solution is to run a Docker Container, defined by the docker-compose.yml file. For a new project that has been just initialised you won't need to change anything here.

Under the project directory run the following command:

docker-compose pull && docker-compose up

It may take some time to download the required packages (@subql/node (opens new window), @subql/query (opens new window), and Postgres) for the first time but soon you'll see a running SubQuery node.

# Running an Indexer (subql/node)


A SubQuery node is an implementation that extracts substrate-based blockchain data per the SubQuery project and saves it into a Postgres database.

# Installation

npm install -g @subql/node

Please note that we DO NOT encourage the use of yarn global due to its poor dependency management which may lead to an errors down the line.

Once installed, you can start a node with the following command:

subql-node <command>

# Key Commands

The following commands will assist you to complete the configuration of a SubQuery node and begin indexing. To find out more, you can always run --help.

# Point to local project path

subql-node -f your-project-path

# Using a Dictionary

Using a full chain dictionary can dramatically speed up the processing of a SubQuery project during testing or during your first index. In some cases, we've seen indexing performance increases of up to 10x.

A full chain dictionary pre-indexes the location of all events and extrinsics within the specific chain and allows your node service to skip to relevant locations when indexing rather than inspecting each block.

You can add the dictionary endpoint in your project.yaml file (see Manifest File), or specify it at run time using the following command:

subql-node --network-dictionary=https://api.subquery.network/sq/subquery/dictionary-polkadot

# Connect to database

export DB_USER=postgres
export DB_PASS=postgres
export DB_DATABASE=postgres
export DB_HOST=localhost
export DB_POST=5432
subql-node -f your-project-path 

Depending on the configuration of your Postgres database (e.g. a different database password), please ensure also that both the indexer (subql/node) and the query service (subql/query) can establish a connection to it.

# Specify a configuration file

subql-node -c your-project-config.yml

This will point the query node to a configuration file which can be in YAML or JSON format. Check out the example below.

subquery: ../../../../subql-example/extrinsics
subqueryName: extrinsics

# Change the block fetching batch size

subql-node -f your-project-path --batch-size 200

[IndexerManager] fetch block [203, 402]
[IndexerManager] fetch block [403, 602]

When the indexer first indexes the chain, fetching single blocks will significantly decrease the performance. Increasing the batch size to adjust the number of blocks fetched will decrease the overall processing time. The current default batch size is 100.

# Local mode

subql-node -f your-project-path --local

For debugging purposes, users can run the node in local mode. Switching to local model will create Postgres tables in the default schema public.

If local mode is not used, a new Postgres schema with the initial subquery_ and corresponding project tables will be created.

# Running a Query Service (subql/query)

# Installation

npm install -g @subql/query

Please note that we DO NOT encourage the use of yarn global due to its poor dependency management which may lead to an errors down the line.

# Running the Query service

export DB_HOST=localhost
subql-query --name <project_name> --playground

Make sure the project name is the same as the project name when you initialize the project. Also, check the environment variables are correct.

After running the subql-query service successfully, open your browser and head to http://localhost:3000. You should see a GraphQL playground showing in the Explorer and the schema that is ready to query.

Last Updated: 8/4/2021, 1:14:57 AM