archive
The archive
program is used to index all blocks, state, and transaction data for a Substrate-based chain and store the indexed data in a relational SQL database.
The database created by the archive
program mirrors all data from a running Substrate blockchain.
After you archive the data, you can use database tools to query and retrieve information from the SQL database about the blockchain state.
For examples of queries you might want to run against a Substrate archive database, see Useful queries.
Before you begin
Before you use archive
to create a database for a Substrate-based chain, you need to prepare your environment with the required files:
-
You must have PostgreSQL installed on the computer where you are running a Substrate node.
You can download PostgreSQL packages for different platforms from the PostgreSQL Downloads page.
Depending on your platform, you might be able to install PostgreSQL using a local package manager. For example, you can install a PostgreSQL package on a macOS computer by running
brew install postgresql
in a Terminal. -
You must have RabbitMQ or Docker Compose installed on the computer where you have PostgreSQL installed.
Depending on your platform, the instruction and system requirements for installing RabbitMQ or Docker can vary. For information about using RabbitMQ or Docker, see the Setup
substrate-archive
wiki page. - Your Substrate chain must use RocksDB as its backend database.
Install and configure
To install the substrate-archive-cli
program:
- Open a terminal shell on your computer.
-
Clone the
substrate-archive
repository by running the following command:git clone https://github.com/paritytech/substrate-archive.git
-
Change to the root directory of the
substrate-archive
repository by running the following command:cd substrate-archive
-
Start the PostgreSQL database (
postgres
) and Postgre administrative process (pgadmin
) on the Substrate node.If you have Docker Compose, you can start the services automatically by running the
docker-compose up -d
command. -
Start your Substrate node, with
pruning
set to archive.For example:
./target/release/node-template --pruning=archive
- Look at the current DBs:
psql -U postgres -hlocalhost -p6432
- Run
DATABASE_URL=postgres://postgres:123@localhost:6432/local_chain_db sqlx
database create insubstrate-archive/src
to create the database. - Set
CHAIN_DATA_DB="<your_path>"
. -
Set up your
archive.conf
file:- make sure to set your base bath to primary DB
- tell it where the rocksdb is. State using CHAINDATADB
- secondary DB is an optimization
- postgres url (set to var if in prod)
- (Optional) setup up logging and debugging.
- Run a node template. Make sure you run it in
--release --dev base-path=/tmp/dir --pruning=archive
- Make a transaction with your node template.
- Start up the
substrate-archive
node for your target chain:cargo run --release -- -c archive-conf.toml --chain=polkadot
- Open a web browser and log in to the Postgres administrative console.
- Default URL: localhost:16543
- Default user name: pgadmin4@pgadmin.org
- Default password: admin
- Look at the reference to start making your queries.