Search
  • cryptoginger29

How to setup cardano-db-sync?

Motivation

I wanted to pull some data from the cardano blockchain, and after some extensive research I wasn't being able to find any places that had a open API to call I decided to put a cardano-db-sync up myself, so that I could start to get the data I wanted.


After trying to follow the official guides, and falling into the different traps and not being able to find any guides that helped, I decided I would make the guide that I needed. So following the steps laid out below, you should be able to set up your cardano-db-sync on one of your nodes.


My plan for the future is to set up the API service that I was looking for to enable Non-cardano people to analyze the block chain and hopefully provide more transparency to the network for the uninitiated.


Requirements:


Installing PostgreSQL dependencies



Since the database we are setting up is a PostgreSQL database we need to install the dependencies for PostgreSQL on the node


sudo apt update
sudo apt install postgresql postgresql-contrib

After installing the PostgreSQL dependencies we need to added the current user as a superuser in PostgreSQL, this is used when setting up the cardano-db-sync later:


sudo -u postgres createuser -s -i -d -r -l -w whoami

You can choose to add a password to your user, it isn't required, but always a good practice.


Installing nix build

The build of the the cardano-db-sync is done using the nix library, so following the official guide, run the following:

curl -L https://nixos.org/nix/install > install-nix.sh
chmod +x install-nix.sh
./install-nix.sh

Setting up IOHK binary cache

This is a REALLY good idea to do, since it will save you hours of waiting, but not a requirement. I tried it without the cache setup and after running for 6 hours it still didn't work, but after setting up the cache it worked.


sudo mkdir -p /etc/nix
cat <<EOF | sudo tee /etc/nix/nix.conf
substituters = https://cache.nixos.org https://hydra.iohk.io
trusted-public-keys = iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
EOF

Setting up the db-sync node

Following the official guide once again we go to home-dir and run the following command

cd ${HOME}
git clone https://github.com/input-output-hk/cardano-db-sync
cd cardano-db-sync
nix-build -A cardano-db-sync -o db-sync-node

Change the rights to the password file, other with the creation of the database will create a warning:

sudo chmod 700 ./config/pgpass-mainnet

We then create the database

PGPASSFILE=config/pgpass-mainnet scripts/postgresql-setup.sh --createdb

This should yield a "ALL GOOD!" message.


Change the node config path in the config file to point to the cardano nodes config file. If you've followed the CoinCashew guide you should be able to get the path using the following command


echo ${NODE_HOME}/main-config.json

Expected output:


/home/{{user}}/cardano-my-node/main-config.json

We then need to added the config path to the cardano node config file in the config file for the db-sync node:


cd config/
nano mainnet-config.yaml

And lastly we sync it, with some customization to the official script:

PGPASSFILE=config/pgpass-mainnet db-sync-node/bin/cardano-db-sync \
    --config config/mainnet-config.yaml \
    --socket-path ${NODE_HOME}/db/socket \
    --state-dir ledger-state/mainnet \
    --schema-dir schema/

And boom!



You should now see your cardano-db-sync node synchronizing with the cardano-my-node.


Resources:

Coin cashew installation guide:

https://github.com/coincashew/coincashew/blob/master/coins/overview-ada/guide-how-to-build-a-haskell-stakepool-node/README.md

Official guide:

https://github.com/input-output-hk/cardano-db-sync

Stackoverflow credits:

https://stackoverflow.com/a/57997449



191 views0 comments

Recent Posts

See All