Table of Contents

PostgreSQL

This connector simplifies accessing PostgreSQL databases. It supports the following .NET drivers:

The remainder of this page assumes you're familiar with the basic concepts of Steeltoe Connectors.

Usage

To use this connector:

  1. Create a PostgreSQL server instance or use a docker container.
  2. Add NuGet references to your project.
  3. Configure your connection string in appsettings.json.
  4. Initialize the Steeltoe Connector at startup.
  5. Use the driver-specific connection/client instance.

Add NuGet References

To use this connector, add a NuGet reference to Steeltoe.Connectors. If you're using Entity Framework Core, add a NuGet reference to Steeltoe.Connectors.EntityFrameworkCore instead.

Also add a NuGet reference to one of the .NET drivers listed above, as you would if you were not using Steeltoe.

Configure connection string

The available connection string parameters for PostgreSQL are documented here.

The following example appsettings.json uses the docker container from above:

{
  "Steeltoe": {
    "Client": {
      "PostgreSql": {
        "Default": {
          "ConnectionString": "Server=localhost;Database=steeltoe;Uid=steeltoe;Pwd=steeltoe"
        }
      }
    }
  }
}

Initialize Steeltoe Connector

Update your Program.cs as below to initialize the Connector:

using Steeltoe.Connectors.PostgreSql;

var builder = WebApplication.CreateBuilder(args);
builder.AddPostgreSql();

Use NpgsqlConnection

To obtain an NpgsqlConnection instance in your application, inject the Steeltoe factory in a controller or view:

using Microsoft.AspNetCore.Mvc;
using Npgsql;
using Steeltoe.Connectors;
using Steeltoe.Connectors.PostgreSql;

public class HomeController : Controller
{
    public async Task<IActionResult> Index(
        [FromServices] ConnectorFactory<PostgreSqlOptions, NpgsqlConnection> connectorFactory)
    {
        var connector = connectorFactory.Get();
        await using NpgsqlConnection connection = connector.GetConnection();
        await connection.OpenAsync();

        NpgsqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT 1";
        object? result = await command.ExecuteScalarAsync();

        ViewData["Result"] = result;
        return View();
    }
}

A complete sample app that uses NpgsqlConnection is provided at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/PostgreSql.

Use Entity Framework Core

Start by defining your DbContext class:

public class AppDbContext : DbContext
{
    public DbSet<SampleEntity> SampleEntities => Set<SampleEntity>();

    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }
}

public class SampleEntity
{
    public long Id { get; set; }
    public string? Text { get; set; }
}

Next, call the UseNpgsql() Steeltoe extension method from Program.cs to initialize Entity Framework Core:

using Steeltoe.Connectors.EntityFrameworkCore.PostgreSql;
using Steeltoe.Connectors.PostgreSql;

var builder = WebApplication.CreateBuilder(args);
builder.AddPostgreSql();

builder.Services.AddDbContext<AppDbContext>(
    (serviceProvider, options) => options.UseNpgsql(serviceProvider));

Once you have configured and added your DbContext to the service container, you can inject it and use it in a controller or view:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

public class HomeController : Controller
{
    public async Task<IActionResult> Index([FromServices] AppDbContext appDbContext)
    {
        List<SampleEntity> entities = await appDbContext.SampleEntities.ToListAsync();
        return View(entities);
    }
}

A complete sample app that uses Entity Framework Core with PostgreSQL is provided at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/PostgreSqlEFCore.

Cloud Foundry

This Connector supports the following service brokers:

You can create and bind an instance to your application by using the Cloud Foundry CLI:

# Create PostgreSQL service
cf create-service csb-azure-postgresql small myPostgreSqlService

# Bind service to your app
cf bind-service myApp myPostgreSqlService

# Restage the app to pick up change
cf restage myApp

Kubernetes

This Connector supports the Service Binding Specification for Kubernetes. It can be used through the Bitnami Services Toolkit.

For details on how to use this, see the instructions at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/PostgreSql#running-on-tanzu-application-platform-tap.