Using PostgreSQL
Using PostgreSQL with Dappetizer is really straightforward and can be set up in a few minutes.

Prerequisites

You will need the following for this guide:
  • For the purposes of this guide, we will assume you already have an existing Dappetizer project. Check out our Quick Start guide to learn how to set up one.
  • Docker (if you are planning on running PostgreSQL in Docker) or a full PostgreSQL instance

Setting up a local database

If you don't have a local PostgreSQL instance running, you can start one using Docker:
Unix/MacOS
1
docker run -d \
2
--name dev-postgres \
3
-e POSTGRES_PASSWORD=postgrespassword \
4
-v ${HOME}/postgres-data/:/var/lib/postgresql/data \
5
-p 5432:5432 \
6
postgres
Copied!

Installing PostgreSQL package

The PostgreSQL client package is required when connecting to the database. To install it, run
1
npm install pg
Copied!

Configuring Dappetizer

The configuration should be changed with specific settings for connecting to PostgreSQL. To connect to your local instance, you can replace the existing database section with:
1
database: {
2
type: 'postgres',
3
host: 'localhost',
4
port: 5432,
5
username: 'postgres',
6
password: 'postgrespassword',
7
database: 'postgres',
8
schema: 'indexer',
9
},
Copied!
Notice that the username and password here are stored in plaintext. That is fine for local development, but when running Dappetizer on a server, you should use a local configuration file stored on the server (the file should not be in source control) or make use of environment variables. For example, this is how you can use environment variables:
1
database: {
2
type: 'postgres',
3
host: process.env['MY_HOST_VARIABLE'],
4
port: 5432,
5
username: process.env['MY_USERNAME_VARIABLE'],
6
password: process.env['MY_PASSWORD_VARIABLE'],
7
database: 'postgres',
8
schema: 'indexer',
9
},
Copied!

Checking the generated data

You can use any tool (like pgAdmin) to connect to the database. In this guide we will simply use Docker to run psql:
1
docker run -it --rm --network=host postgres psql -h localhost -U postgres
Copied!
To select what blocks have been saved by the indexer, we will run a short SELECT query:
1
select * from indexer.block limit 10;
Copied!
If the indexer has successfully connected to the database and started indexing, you should be able to see the stored blocks so far.