{"id":3051,"date":"2021-06-26T00:42:00","date_gmt":"2021-06-25T12:42:00","guid":{"rendered":"https:\/\/www.talkcrypto.org\/blog\/?p=3051"},"modified":"2023-11-28T01:22:55","modified_gmt":"2023-11-27T12:22:55","slug":"how-to-run-a-subquery-indexer","status":"publish","type":"post","link":"https:\/\/www.talkcrypto.org\/blog\/2021\/06\/26\/how-to-run-a-subquery-indexer\/","title":{"rendered":"How to run a SubQuery Indexer?"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>At first, it may sound confusing but running an indexer is synonymous to running a node which will &#8220;index&#8221; the blockchain and store the data in a database. To be more precise, the @subql\/node implementation will extract substrate-based blockchain data (aka on the PolkaDot network) and save it into a Postgres database. <\/p>\n\n\n\n<p>Currently you don&#8217;t &#8220;join&#8221; a network. You create a SubQuery project and &#8220;run&#8221; it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Postgres<\/h2>\n\n\n\n<p>The first thing you will need to do is to install Postgres. Visit <a rel=\"noreferrer noopener\" href=\"https:\/\/www.postgresql.org\/download\/\" target=\"_blank\">https:\/\/www.postgresql.org\/download\/<\/a> and install version 12 or higher.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"703\" height=\"254\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image.png\" alt=\"\" class=\"wp-image-3052\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image.png 703w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-300x108.png 300w\" sizes=\"(max-width: 703px) 100vw, 703px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SubQuery node<\/h2>\n\n\n\n<p>To install the SubQuery node application, run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g @subql\/node<\/code><\/pre>\n\n\n\n<p>The -g flag means to install it globally which means on OSX, the location will be \/usr\/local\/lib\/node_modules<\/p>\n\n\n\n<p>Once installed, check out the possible flags with the help command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; subql-node --help\nOptions:\n      --help                Show help                                  &#91;boolean]\n      --version             Show version number                        &#91;boolean]\n  -f, --subquery            Local path of the subquery project          &#91;string]\n      --subquery-name       Name of the subquery project                &#91;string]\n  -c, --config              Specify configuration file                  &#91;string]\n      --local               Use local mode                             &#91;boolean]\n      --batch-size          Batch size of blocks to fetch in one round  &#91;number]\n      --timeout             Timeout for indexer sandbox to execute the mapping\n                            functions                                   &#91;number]\n      --debug               Show debug information to console output. will\n                            forcefully set log level to debug\n                                                      &#91;boolean] &#91;default: false]\n      --profiler            Show profiler information to console output\n                                                      &#91;boolean] &#91;default: false]\n      --network-endpoint    Blockchain network endpoint to connect      &#91;string]\n      --output-fmt          Print log as json or plain text\n                                           &#91;string] &#91;choices: \"json\", \"colored\"]\n      --log-level           Specify log level to print. Ignored when --debug is\n                            used\n          &#91;string] &#91;choices: \"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\",\n                                                                       \"silent\"]\n      --migrate             Migrate db schema (for management tables only)\n                                                      &#91;boolean] &#91;default: false]\n      --timestamp-field     Enable\/disable created_at and updated_at in schema\n                                                       &#91;boolean] &#91;default: true]\n  -d, --network-dictionary  Specify the dictionary api for this network &#91;string]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">#Connecting to a database<\/h2>\n\n\n\n<p>Use the export command to set the environment variables as shown below. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export DB_USER=postgres\nexport DB_PASS=postgres\nexport DB_DATABASE=postgres\nexport DB_HOST=localhost\nexport DB_PORT=5432<\/code><\/pre>\n\n\n\n<p>Typing &#8220;env&#8221; should list your current environment variables and note that these variables are only set temporarily. In other words, they are only valid for the duration that the terminal window is open for. You can make it more permanent by creating these in your .\/bash_profile. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Starting a project<\/h2>\n\n\n\n<p>To start a project, use the -f flag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subql-node -f &lt;project_path&gt;<\/code><\/pre>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subql-node -f . \/\/for current directory\nsubql-node -f .\/helloworld \/\/ for helloworld directory \n\n2021-08-09T23:24:08.778Z &lt;fetch&gt; INFO fetch block &#91;6401,6500], total 100 blocks \n2021-08-09T23:24:11.807Z &lt;fetch&gt; INFO fetch block &#91;6501,6600], total 100 blocks \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Postgres<\/h2>\n\n\n\n<p>If you open up Postgres, you&#8217;ll see that two tables are created. public.subqueries and subquery_1.starter_entities<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"345\" height=\"775\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-14-at-11.18.31-am-1.png\" alt=\"\" class=\"wp-image-3075\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-14-at-11.18.31-am-1.png 345w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-14-at-11.18.31-am-1-134x300.png 134w\" sizes=\"(max-width: 345px) 100vw, 345px\" \/><\/figure>\n\n\n\n<p>The starter_entities table contains the indexes. To view the data, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from subquery_1.starter_entities<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"449\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-2-1024x449.png\" alt=\"\" class=\"wp-image-3076\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-2-1024x449.png 1024w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-2-300x131.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-2-768x337.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-2.png 1328w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>public.subqueries only contains 1 row which the node checks upon start up to &#8220;understand the current state&#8221; so it knows where to continue from so to speak. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"314\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-3-1024x314.png\" alt=\"\" class=\"wp-image-3077\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-3-1024x314.png 1024w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-3-300x92.png 300w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-3-768x236.png 768w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-3-1536x471.png 1536w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-3.png 1884w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">SubQuery query service<\/h1>\n\n\n\n<p>Now that we have the node running, the next step is to get the query service up and running. Run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g @subql\/query<\/code><\/pre>\n\n\n\n<p>To start the service, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subql-query --name subql-helloworld --playground<\/code><\/pre>\n\n\n\n<p>If you get this error message, &#8220;Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string&#8221;, this means that you need to set your DB_PASS again. This could be the result of you opening up another terminal meaning the environment variables need to be set again. <\/p>\n\n\n\n<p>Also make sure to set your DB_USER via:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export DB_USER=postgres<\/code><\/pre>\n\n\n\n<p>The next error you will probably encounter is &#8220;Error: listen EADDRINUSE: address already in use :::3000&#8221;. <\/p>\n\n\n\n<p>Set a different PORT variable via:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PORT=3001<\/code><\/pre>\n\n\n\n<p>You should then see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/Code\/subQuery\/projects\/subql-helloworld$ export PORT=3002\n~\/Code\/subQuery\/projects\/subql-helloworld$ subql-query --name subql-helloworld --playground\n(node:20940) &#91;PINODEP007] Warning: bindings.level is deprecated, use options.level option instead\n(Use `node --trace-warnings ...` to show where the warning was created)\n2021-08-14T02:29:58.525Z &lt;nestjs&gt; INFO Starting Nest application... \n2021-08-14T02:29:58.534Z &lt;nestjs&gt; INFO AppModule dependencies initialized \n2021-08-14T02:29:58.534Z &lt;nestjs&gt; INFO ConfigureModule dependencies initialized \n2021-08-14T02:29:58.534Z &lt;nestjs&gt; INFO GraphqlModule dependencies initialized \n\u26a0\ufe0f WARNING\u26a0\ufe0f  You requested to use schema 'subquery_1'; however we couldn't find some of those! Missing schemas are: 'subquery_1'\n2021-08-14T02:29:58.752Z &lt;nestjs&gt; INFO Nest application successfully started \n(node:20940) &#91;PINODEP007] Warning: bindings.level is deprecated, use options.level option instead<\/code><\/pre>\n\n\n\n<p>Then navigate to http:\/\/localhost:3002\/ and you should see&#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"254\" src=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-4.png\" alt=\"\" class=\"wp-image-3080\" srcset=\"https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-4.png 631w, https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/08\/image-4-300x121.png 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/figure>\n\n\n\n<p>Then run your query as normal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At first, it may sound confusing but running an indexer<\/p>\n","protected":false},"author":1,"featured_media":3423,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blockchain"],"featured_image_urls":{"full":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery.png",1800,773,false],"thumbnail":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-150x150.png",150,150,true],"medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-300x129.png",300,129,true],"medium_large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-768x330.png",640,275,true],"large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-1024x440.png",640,275,true],"1536x1536":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-1536x660.png",1536,660,true],"2048x2048":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery.png",1800,773,false],"chromenews-featured":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-1024x440.png",1024,440,true],"chromenews-large":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-825x575.png",825,575,true],"chromenews-medium":["https:\/\/www.talkcrypto.org\/blog\/wp-content\/uploads\/2021\/06\/subquery-590x410.png",590,410,true]},"author_info":{"info":["seandotau"]},"category_info":"<a href=\"https:\/\/www.talkcrypto.org\/blog\/category\/blockchain\/\" rel=\"category tag\">Blockchain<\/a>","tag_info":"Blockchain","comment_count":"0","_links":{"self":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/3051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/comments?post=3051"}],"version-history":[{"count":6,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/3051\/revisions"}],"predecessor-version":[{"id":3097,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/posts\/3051\/revisions\/3097"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media\/3423"}],"wp:attachment":[{"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/media?parent=3051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/categories?post=3051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.talkcrypto.org\/blog\/wp-json\/wp\/v2\/tags?post=3051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}