| name | postgresql |
| description | Manages PostgreSQL databases and Entity Framework Core integration.
Use when: configuring database connections, writing migrations, creating DbContext classes, optimizing queries, or integrating with .NET Aspire.
|
| allowed-tools | Read, Edit, Write, Glob, Grep, Bash, mcp__context7__resolve-library-id, mcp__context7__query-docs |
PostgreSQL Skill
PostgreSQL database management for Sorcha's decentralised register platform. This project uses PostgreSQL 17 with Npgsql 8.0+ and Entity Framework Core 10, featuring dedicated schemas (wallet, public), JSONB columns for metadata, and .NET Aspire service discovery for connection management.
Quick Start
DbContext with PostgreSQL Schema
public class WalletDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("wallet");
modelBuilder.Entity<Wallet>(entity =>
{
entity.ToTable("wallets");
entity.HasKey(e => e.Address);
entity.Property(e => e.Metadata)
.HasColumnType("jsonb");
entity.HasQueryFilter(e => e.DeletedAt == null);
});
}
}
Npgsql Configuration with Retry
services.AddNpgsqlDataSource(connectionString, dataSourceBuilder =>
{
dataSourceBuilder.EnableDynamicJson();
});
services.AddDbContext<WalletDbContext>(options =>
{
options.UseNpgsql(connectionString, npgsqlOptions =>
{
npgsqlOptions.EnableRetryOnFailure(
maxRetryCount: 10,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorCodesToAdd: null);
npgsqlOptions.MigrationsHistoryTable("__EFMigrationsHistory", "wallet");
});
});
Aspire Service Discovery
var postgres = builder.AddPostgres("postgres").WithPgAdmin();
var walletDb = postgres.AddDatabase("wallet-db", "sorcha_wallet");
builder.AddProject<Projects.Sorcha_Wallet_Service>("wallet-service")
.WithReference(walletDb);
Key Concepts
| Concept | Usage | Example |
|---|
| Schema isolation | Separate domains | modelBuilder.HasDefaultSchema("wallet") |
| JSONB columns | Flexible metadata | HasColumnType("jsonb") |
| Soft deletes | Query filters | HasQueryFilter(e => e.DeletedAt == null) |
| Row versioning | Concurrency | Property(e => e.RowVersion).IsRowVersion() |
| Composite indexes | Query performance | HasIndex(e => new { e.Owner, e.Tenant }) |
| UUID generation | Primary keys | HasDefaultValueSql("gen_random_uuid()") |
Common Patterns
High-Precision Decimal for Crypto
When: Storing cryptocurrency amounts
entity.Property(e => e.Balance)
.HasColumnType("numeric(28,18)")
.HasPrecision(28, 18);
Timestamp with Timezone
When: Audit trails with timezone awareness
entity.Property(e => e.CreatedAt)
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
See Also
- patterns - DbContext configuration, indexing strategies
- workflows - Migrations, health checks, initialization
Related Skills
- entity-framework - ORM patterns and repository implementations
- aspire - Service discovery and connection string injection
- docker - PostgreSQL container configuration
Documentation Resources
Fetch latest PostgreSQL documentation with Context7.
How to use Context7:
- Use
mcp__context7__resolve-library-id to search for "postgresql" or "npgsql efcore"
- Prefer website documentation (IDs starting with
/websites/) over source code repositories
- Query with
mcp__context7__query-docs using the resolved library ID
Library IDs:
- PostgreSQL:
/websites/postgresql (61K snippets, High reputation)
- Npgsql EF Core:
/websites/npgsql_efcore_index_html
- EF Core:
/dotnet/entityframework.docs (4K snippets, High reputation)
Recommended Queries:
- "Index creation strategies"
- "JSONB operators and functions"
- "Connection pooling configuration"
- "Query performance optimization"