DB-Sync
By IOG
This page provides instructions on how to interact with the Cardano DB Sync service offered by Demeter.
DB Sync follows the Cardano chain and takes information from the chain and an internally maintained copy of the ledger state. Data is then extracted from the chain and inserted into a PostgreSQL database. SQL queries can then be written directly against the database schema or as queries embedded in any language with libraries for interacting with an SQL database. DB-Sync is a tool developed by IntersectMBO under Apache 2.0 license. Examples, schema, and documentation can be found in the GitHub repository.
Each Demeter cluster provides highly-available, multi-tenant instances of Cardano DB Sync. Queries to these instances are load balanced. Projects can access DB Sync data by connecting directly to the PostgreSQL engine.
Demeter DB Sync access will consume DCUs from your project's wallet proportional to the volume and duration of the queries executed against the PostgreSQL instance. If no queries are executed, Demeter will not charge anything.
To enable access in your project, you need to add a Cardano DB Sync Port resource:
Once created, your Cardano DB Sync Port resource will present you with the required information to access the PostgreSQL instance:
dbynsc-v3.demeter.run
)5432
)dbsync-mainnet
)dmtr_dbsync1jjv9pyqyna
)The above value will change for each project depending on your configuration.
You can use a library like psycopg2
to connect to a PostgreSQL instance in Python. Here's a basic example:
import psycopg2
# replace the following values with the ones provided in Demeter's console
connection = psycopg2.connect(
host="dbsync-v3.demeter.run",
port=5432,
user="dmtr_dbsync1jjv9pyqyna",
password="your_password",
dbname="dbsync-mainnet",
)
# Create a cursor object
cursor = connection.cursor()
# Execute a query
cursor.execute("SELECT * FROM block ORDER BY id DESC LIMIT 10")
# Fetch and print the results
rows = cursor.fetchall()
for row in rows:
print(row)
In this code, replace host
, port
, user
, password
and dbname
with your actual PostgreSQL details that you can find on Demeter's resource detail.
This will connect to the PostgreSQL server, run a query to fetch the latest 5 blocks in the DBSync, and print them to the console.