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:
1
sudo apt-get remove docker docker-engine docker.io containerd runc
Copied!
Update the package repository:
1
sudo apt-get update
Copied!
Install the necessary packages for the repositories over https:
1
sudo apt-get install \
2
apt-transport-https \
3
ca-certificates \
4
curl \
5
gnupg-agent \
6
software-properties-common
Copied!
Add the official Docker GPG key:
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Copied!
Verify the key:
1
sudo apt-key fingerprint 0EBFCD88
Copied!
Set up the stable Docker repository.
1
sudo add-apt-repository \
2
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
3
$(lsb_release -cs) \
4
stable"
Copied!
Finally we update the repo (to get the most recent version) and install it.
1
sudo apt-get update
2
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose
Copied!

Install cardano-db-sync

Download the source code of cardano-db-sync project:
1
git clone https://github.com/input-output-hk/cardano-db-sync.git
2
cd cardano-db-sync
Copied!
Lets check the configuration with which the new environment will be launch:
1
docker-compose config
Copied!
My personal configuration is as follows:
1
version: "3.5"
2
3
services:
4
postgres:
5
image: postgres:11.5-alpine
6
shm_size: 1g
7
environment:
8
- POSTGRES_LOGGING=true
9
- POSTGRES_DB_FILE=/run/secrets/postgres_db
10
- POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
11
- POSTGRES_USER_FILE=/run/secrets/postgres_user
12
secrets:
13
- postgres_password
14
- postgres_user
15
- postgres_db
16
volumes:
17
- postgres:/var/lib/postgresql/data
18
ports:
19
- 5432:5432
20
restart: on-failure
21
logging:
22
driver: "json-file"
23
options:
24
max-size: "200k"
25
max-file: "10"
26
27
cardano-node:
28
image: inputoutput/cardano-node:1.19.0
29
environment:
30
- NETWORK=${NETWORK:-mainnet}
31
volumes:
32
- node-db:/data/db
33
- node-ipc:/ipc
34
restart: on-failure
35
logging:
36
driver: "json-file"
37
options:
38
max-size: "200k"
39
max-file: "10"
40
41
cardano-db-sync:
42
image: inputoutput/cardano-db-sync:4.0.0
43
environment:
44
- NETWORK=${NETWORK:-mainnet}
45
- POSTGRES_HOST=postgres
46
- POSTGRES_PORT=5432
47
depends_on:
48
- cardano-node
49
- postgres
50
secrets:
51
- postgres_password
52
- postgres_user
53
- postgres_db
54
volumes:
55
- node-ipc:/node-ipc
56
restart: on-failure
57
logging:
58
driver: "json-file"
59
options:
60
max-size: "200k"
61
max-file: "10"
62
63
secrets:
64
postgres_db:
65
file: ./config/secrets/postgres_db
66
postgres_password:
67
file: ./config/secrets/postgres_password
68
postgres_user:
69
file: ./config/secrets/postgres_user
70
71
volumes:
72
postgres:
73
node-db:
74
node-ipc:
Copied!
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.
1
cp ~/home/cardano/cardano-db-sync/config/secrets/postgres_db_example \
2
~/home/cardano/cardano-db-sync/config/secrets/postgres_db
Copied!
1
cp ~/home/cardano/cardano-db-sync/config/secrets/postgres_password_example \
2
~/home/cardano/cardano-db-sync/config/secrets/postgres_password
Copied!
Remember change the password.
1
cp ~/home/cardano/cardano-db-sync/config/secrets/postgres_user_example \
2
~/home/cardano/cardano-db-sync/config/secrets/postgres_user
Copied!
Before running Docker we need to add our user to the docker group:
1
sudo usermod -aG docker $USER
Copied!
Start the instance with the log messages activated:
1
docker-compose up -d && docker-compose logs -f
Copied!
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
Create the script that starts the service in the background:
1
cd /home/cardano/cardano-db-sync
2
vim start.sh
Copied!
1
#!/bin/bash
2
session="cardano-db-sync"
3
# Check if the session exists, discarding output
4
# We can check $? for the exit status (zero for success, non-zero for failure)
5
tmux has-session -t $session 2>/dev/null
6
if [ $? != 0 ]; then
7
tmux attach-session -t $session
8
tmux new -s "cardano-db-sync" -n "node" -d
9
tmux split-window -v
10
tmux select-pane -t 'cardano-db-sync:node.0'
11
tmux split-window -h
12
tmux send-keys -t 'cardano-db-sync:node.0' './init-cardano-db.sh' Enter
13
tmux send-keys -t 'cardano-db-sync:node.1' 'htop' Enter
14
tmux send-keys -t 'cardano-db-sync.2' 'nload' Enter
15
fi
16
17
tmux attach-session -t $session
Copied!
Create the script that executes docker:
1
cd /home/cardano/cardano-db-sync
2
vim init-cardano-db.sh
Copied!
1
#!/bin/bash
2
docker-compose up -d && docker-compose logs -f
Copied!
Create the script that ends the service associated with the "cardano-db-sync" session.
1
cardano-db-sync
Copied!
1
vim stop.sh
Copied!
1
#!/bin/bash
2
# Check if the session exists, discarding output
3
# We can check $? for the exit status (zero for success, non-zero for failure)
4
session="cardano-db-sync"
5
# Check if the session exists, discarding output
6
# We can check $? for the exit status (zero for success, non-zero for failure)
7
8
tmux has-session -t $session 2>/dev/null
9
if [ $? != 0 ]; then
10
echo "Session not found."
11
else
12
echo "Killing session"
13
tmux kill-session -t cardano-db-sync
14
fi
Copied!
Add execution permissions to the scripts:
1
chmod +x start.sh stop.sh init-cardano-db.sh
Copied!
Finally, start the service:
1
./start.sh
Copied!
Once started we can launch queries to the address localhost: 5432.
List all the containers:
1
docker container ls -a
Copied!
Delete a specific container by its ID:
1
docker container rm 661525b875a1
Copied!
Terminate all instances:
1
docker-compose down -v
Copied!
Relaunch the instances:
1
docker-compose up --remove-orphans --force-recreate
Copied!
Support our work delegating to Ada Booster.
Copiar enlace