Querying the blockchain

To start an instance of cardano-db-sync we are going to use Docker as manager and deployment of this instance. Before continuing make sure you have Docker installed.

First we uninstall possible old versions of Docker:

sudo apt-get remove docker docker-engine docker.io containerd runc

Update the package repository:

sudo apt-get update

Install the necessary packages for the repositories over https:

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

Add the official Docker GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify the key:

sudo apt-key fingerprint 0EBFCD88

Set up the stable Docker repository.

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Finally we update the repo (to get the most recent version) and install it.

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose

Install cardano-db-sync

Download the source code of cardano-db-sync project:

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

Lets check the configuration with which the new environment will be launch:

docker-compose config

My personal configuration is as follows:

version: "3.5"
services:
postgres:
image: postgres:11.5-alpine
shm_size: 1g
environment:
- POSTGRES_LOGGING=true
- POSTGRES_DB_FILE=/run/secrets/postgres_db
- POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
- POSTGRES_USER_FILE=/run/secrets/postgres_user
secrets:
- postgres_password
- postgres_user
- postgres_db
volumes:
- postgres:/var/lib/postgresql/data
ports:
- 5432:5432
restart: on-failure
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
cardano-node:
image: inputoutput/cardano-node:1.19.0
environment:
- NETWORK=${NETWORK:-mainnet}
volumes:
- node-db:/data/db
- node-ipc:/ipc
restart: on-failure
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
cardano-db-sync:
image: inputoutput/cardano-db-sync:4.0.0
environment:
- NETWORK=${NETWORK:-mainnet}
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
depends_on:
- cardano-node
- postgres
secrets:
- postgres_password
- postgres_user
- postgres_db
volumes:
- node-ipc:/node-ipc
restart: on-failure
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
secrets:
postgres_db:
file: ./config/secrets/postgres_db
postgres_password:
file: ./config/secrets/postgres_password
postgres_user:
file: ./config/secrets/postgres_user
volumes:
postgres:
node-db:
node-ipc:

If we look at the configuration we see that Docker is going to use the postgres_db, postgres_password and postgres_user credentials. It will look for them in the /home/cardano/cardano-db-sync/config/secrets folder. The default repository brings these files under the suffix * _example, we just have to replace or duplicate the examples.

cp ~/home/cardano/cardano-db-sync/config/secrets/postgres_db_example \
~/home/cardano/cardano-db-sync/config/secrets/postgres_db
cp ~/home/cardano/cardano-db-sync/config/secrets/postgres_password_example \
~/home/cardano/cardano-db-sync/config/secrets/postgres_password

Remember change the password.

cp ~/home/cardano/cardano-db-sync/config/secrets/postgres_user_example \
~/home/cardano/cardano-db-sync/config/secrets/postgres_user

Before running Docker we need to add our user to the docker group:

sudo usermod -aG docker $USER

Start the instance with the log messages activated:

docker-compose up -d && docker-compose logs -f

With this we have started a cardano-node services, the postgresql database, and the cardano-db-sync tool. We can access these services through the default port: 5432.

A more optimal execution

We are going to use tmux to run docker in the background and show the machine status live.

start.sh
init-cardano-db.sh
stop.sh
start.sh

Create the script that starts the service in the background:

cd /home/cardano/cardano-db-sync
vim start.sh
#!/bin/bash
session="cardano-db-sync"
# Check if the session exists, discarding output
# We can check $? for the exit status (zero for success, non-zero for failure)
tmux has-session -t $session 2>/dev/null
if [ $? != 0 ]; then
tmux attach-session -t $session
tmux new -s "cardano-db-sync" -n "node" -d
tmux split-window -v
tmux select-pane -t 'cardano-db-sync:node.0'
tmux split-window -h
tmux send-keys -t 'cardano-db-sync:node.0' './init-cardano-db.sh' Enter
tmux send-keys -t 'cardano-db-sync:node.1' 'htop' Enter
tmux send-keys -t 'cardano-db-sync.2' 'nload' Enter
fi
tmux attach-session -t $session
init-cardano-db.sh

Create the script that executes docker:

cd /home/cardano/cardano-db-sync
vim init-cardano-db.sh
#!/bin/bash
docker-compose up -d && docker-compose logs -f
stop.sh

Create the script that ends the service associated with the "cardano-db-sync" session.

cardano-db-sync
vim stop.sh
#!/bin/bash
# Check if the session exists, discarding output
# We can check $? for the exit status (zero for success, non-zero for failure)
session="cardano-db-sync"
# Check if the session exists, discarding output
# We can check $? for the exit status (zero for success, non-zero for failure)
tmux has-session -t $session 2>/dev/null
if [ $? != 0 ]; then
echo "Session not found."
else
echo "Killing session"
tmux kill-session -t cardano-db-sync
fi

Add execution permissions to the scripts:

chmod +x start.sh stop.sh init-cardano-db.sh

Finally, start the service:

./start.sh

Once started we can launch queries to the address localhost: 5432.

List all the containers:

docker container ls -a

Delete a specific container by its ID:

docker container rm 661525b875a1

Terminate all instances:

docker-compose down -v

Relaunch the instances:

docker-compose up --remove-orphans --force-recreate