Nethereum.BlockchainStore.Postgres
7.0.0
Prefix Reserved
dotnet add package Nethereum.BlockchainStore.Postgres --version 7.0.0
NuGet\Install-Package Nethereum.BlockchainStore.Postgres -Version 7.0.0
<PackageReference Include="Nethereum.BlockchainStore.Postgres" Version="7.0.0" />
<PackageVersion Include="Nethereum.BlockchainStore.Postgres" Version="7.0.0" />
<PackageReference Include="Nethereum.BlockchainStore.Postgres" />
paket add Nethereum.BlockchainStore.Postgres --version 7.0.0
#r "nuget: Nethereum.BlockchainStore.Postgres, 7.0.0"
#:package Nethereum.BlockchainStore.Postgres@7.0.0
#addin nuget:?package=Nethereum.BlockchainStore.Postgres&version=7.0.0
#tool nuget:?package=Nethereum.BlockchainStore.Postgres&version=7.0.0
Nethereum.BlockchainStore.Postgres
PostgreSQL implementation of the Nethereum blockchain storage layer using Entity Framework Core and Npgsql.
Overview
Nethereum.BlockchainStore.Postgres provides the PostgreSQL-specific DbContext, context factory, and DI registration for storing indexed Ethereum blockchain data. It inherits from Nethereum.BlockchainStore.EFCore.BlockchainDbContextBase and configures Npgsql with lowercase naming conventions.
This package is used by the blockchain processing pipeline to persist blocks, transactions, logs, contracts, internal transactions, and chain state to PostgreSQL. It is also used by the Explorer to query indexed data.
Key Features
PostgresBlockchainDbContextconfigured withUseNpgsql()andUseLowerCaseNamingConvention()PostgresBlockchainDbContextFactoryimplementingIBlockchainDbContextFactoryfor short-lived context creationAddPostgresBlockchainStorage()DI extension that registers the factory and all EFCore repositories- EF Core migrations for the full blockchain schema (blocks, transactions, logs, contracts, internal transactions, chain state, account state, address transactions)
- Design-time factory for
dotnet ef migrationstooling
Installation
dotnet add package Nethereum.BlockchainStore.Postgres
Targets net8.0 and net10.0. Uses Npgsql.EntityFrameworkCore.PostgreSQL 8.x on net8.0 and 10.x on net10.0.
Dependencies
- Nethereum.BlockchainStore.EFCore - Base
BlockchainDbContextBase, entity builders, and repository implementations - Npgsql.EntityFrameworkCore.PostgreSQL - PostgreSQL EF Core provider
- EFCore.NamingConventions -
UseLowerCaseNamingConvention()for lowercase table/column names - Microsoft.EntityFrameworkCore.Design - Design-time migration support (private asset)
Quick Start
using Nethereum.BlockchainStore.Postgres;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("PostgresConnection");
builder.Services.AddPostgresBlockchainStorage(connectionString);
This single call registers:
IBlockchainDbContextFactoryasPostgresBlockchainDbContextFactory- All repository implementations via
AddBlockchainRepositories()(IBlockRepository,ITransactionRepository,ITransactionLogRepository,IContractRepository, etc.)
Usage Examples
Run Migrations
cd src/Nethereum.BlockchainStore.Postgres
dotnet ef migrations add InitialCreate \
--context PostgresBlockchainDbContext
dotnet ef database update \
--context PostgresBlockchainDbContext
The design-time factory (PostgresBlockchainDesignTimeDbContextFactory) is used only by the dotnet ef tooling and returns a context with a hardcoded local connection string (Host=localhost;Database=design;Username=postgres;Password=postgres). It does not read appsettings.json. Point your tooling at a scratch design database, or edit the factory, when generating migrations.
Programmatic Migration
var factory = new PostgresBlockchainDbContextFactory(connectionString);
using var context = factory.CreateContext();
await context.Database.MigrateAsync();
Direct Context Usage
var factory = new PostgresBlockchainDbContextFactory(connectionString);
using var context = factory.CreateContext();
var latestBlock = await context.Blocks
.Where(b => b.IsCanonical)
.OrderByDescending(b => b.BlockNumber)
.FirstOrDefaultAsync();
var txCount = await context.Transactions
.Where(t => t.BlockNumber == latestBlock.BlockNumber && t.IsCanonical)
.CountAsync();
With Block Storage Processor
var dbContextFactory = new PostgresBlockchainDbContextFactory(connectionString);
var repoFactory = new BlockchainStoreRepositoryFactory(dbContextFactory);
var steps = new BlockStorageProcessingSteps(repoFactory);
var orchestrator = new BlockCrawlOrchestrator(web3.Eth, steps);
orchestrator.ContractCreatedCrawlerStep.RetrieveCode = true;
var processor = new BlockchainProcessor(
orchestrator,
repoFactory.CreateBlockProgressRepository(),
lastConfirmedBlockService);
await processor.ExecuteAsync(cancellationToken);
Connection String Resolution
The PostgresBlockchainDbContext constructor takes only an explicit connection string. The fallback resolution from configuration is performed by the static PostgresBlockchainDbContextFactory.Create(IConfigurationRoot) method, which resolves in this order:
ConnectionStrings:PostgresConnectionfromappsettings.jsonConnectionStrings:BlockchainDbStoragefromappsettings.json(viaGetBlockchainStorageConnectionString())
Database Schema
All table and column names use lowercase convention via EFCore.NamingConventions.
Tables
| Table | Entity | Key Columns |
|---|---|---|
blocks |
Block |
blocknumber (bigint), hash, parenthash, miner, timestamp (bigint), gasused, gaslimit, basefeepergas, blobgasused, excessblobgas, parentbeaconblockroot, requestshash |
transactions |
Transaction |
blocknumber (bigint), hash, addressfrom, addressto, transactionindex (bigint), timestamp (bigint), value, gas, gasprice, gasused, transactiontype (bigint), maxfeeperblobgas, blobgasused, blobgasprice |
transactionlogs |
TransactionLog |
transactionhash, logindex (bigint), blocknumber (bigint), address, eventhash, data |
contracts |
Contract |
address, name, abi, code, creator, transactionhash |
internaltransactions |
InternalTransaction |
transactionhash, traceindex, blocknumber (bigint), addressfrom, addressto, value, type |
blockprogress |
BlockProgress |
lastblockprocessed |
chainstates |
ChainState |
lastcanonicalblocknumber (bigint, nullable), finalizedblocknumber (bigint, nullable), chainid |
accountstates |
AccountState |
address, balance, nonce (bigint), lastupdatedblock (bigint) |
Numeric fields (blocknumber, timestamp, transactionindex, logindex, nonce, transactiontype) are stored as bigint (long). Gas and value fields remain as character varying(100) strings to accommodate full uint256 range.
Related Packages
Used By (Consumers)
- Nethereum.BlockchainStorage.Processors.Postgres - Hosted services that orchestrate the processing pipeline against this database
- Nethereum.Explorer - Blazor Server explorer that queries indexed data via
IBlockchainDbContextFactory - Nethereum.Aspire.Indexer - Aspire-hosted indexer worker
Dependencies
- Nethereum.BlockchainStore.EFCore - Base DbContext, entity builders, repositories
See Also
- Nethereum.BlockchainStore.EFCore - Database-agnostic EF Core base
- Nethereum.BlockchainStorage.Processors.Postgres - Processing hosted services
- Nethereum.BlockchainProcessing - Processing framework and entity definitions
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- EFCore.NamingConventions (>= 10.0.0)
- Nethereum.BlockchainStore.EFCore (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.3)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 10.0.0)
-
net8.0
- EFCore.NamingConventions (>= 8.0.3)
- Nethereum.BlockchainStore.EFCore (>= 7.0.0)
- Newtonsoft.Json (>= 11.0.2 && < 14.0.0)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.0.10)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Nethereum.BlockchainStore.Postgres:
| Package | Downloads |
|---|---|
|
Nethereum.BlockchainStorage.Processors.Postgres
PostgreSQL-specific DI registration for the Nethereum blockchain indexer hosted services. |
GitHub repositories
This package is not used by any popular GitHub repositories.