Skip to main content
This tutorial demonstrates how to query contracts and transactions using SQL in the Participant Query Store (PQS).

Overview

This tutorial shows how to:
  • connect a PQS instance to a Daml ledger
  • connect a PQS instance to a PostgreSQL database
  • query contracts that get exported with SQL API

Prerequisites

Before running this tutorial, ensure you meet the following prerequisites:

Existing canton

We assume that the referenced tutorial has been completed successfully and that you still have a running Canton instance with a Daml ledger and any created contracts.

Starting PostgreSQL

We will use official PostgreSQL Docker image to run a PostgreSQL instance. The following command will start a PostgreSQL instance with the default user postgres and password postgres:
This command will run PostgreSQL in a detached mode, mapping the container’s port 5432 to the host’s port 5432.

Starting PQS

Now we can start the PQS instance. The following command will run PQS, connecting it to the ledger and the database we just started:
We should observe similar lines in the logs output if all goes well:
This indicates that the PQS instance is successfully connected to the ledger and the database.

Exploring data with SQL

Now that we have all components connected and operational, we can start querying the data using SQL. We will use the psql command-line tool (supplied by the official PostgreSQL Docker image) to connect to the PostgreSQL instance and run SQL queries. In a new terminal, run the following command to connect to the PostgreSQL instance:
Let’s explore the summary of current Active Contract Set (ACS):
We can turn psql into the extended mode to improve readability of the output:
Now we can query the active contracts to see all the details along with the payload:
We can turn off the extended mode by re-running the command:
Let’s now query the data with a more specific filter:

Next steps

In this tutorial, we have successfully connected a PQS instance to a Daml ledger and a PostgreSQL database, and we have explored the ledger data using SQL. You can refer to the following resources for more information: