Reputation: 71
Pre-requisite
Npgsql nuget : "Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" or above or below .
Postgresql-ha:14.2.16 by bitnami : Postgresql HA by Bitnami
Configured the PostgreSQL and Pgpool server using the Bitnami Postgresql HA Helm Chart.
Attempted to connect to the database and initiate data migration using Entity Framework Core. Encountered the Npgsql.PostgresException with the error message indicating a failure to obtain the session context (XX000: unable to get session context).
In our environment we're using postgresql-ha chart , 2 replicas of pgpool , 3 replicas of postgresql server. We use EF core , .
We set global.postgresql.database as 'db1' in our postgres-ha bitnami values.yaml, thisensures the database is created when the chart is installed in a kubernetes cluster. If we were to view the databases using a DB viewer like pgadmin we would see db1. We apply migrations with the help of a helm hook job, which runs as a pre-install job which ensures schemas or databases are created. Although as the database db1 is created , the schemas are created successfully under db1 database as the database already exists. Although now if global.postgresl.database or postgresql.database were empty " " , then in that case db1 database will not be created. As we specify db1 is the database where migrations would need to applied in our connection string, it ideally should be able to create a database. Although when this requests reaches pgpool , pgpool is unable to establish a context as the database is not created. How do i ensure the database is created if my connection string has pgpool
Additional note, in a HA setup of postgres , if we were to use the headless service such postgres-headless.infra.svc.cluster , in this case we wouldn't know which instance of postgres the request reaches it can be either a write replica or a read replica which wouldn't have enough privilege to create a database.
Variables used:
The connection string in _npgsqlConnectionStringBuilder
_npgsqlConnectionStringBuilder.Host = pgpool.infra.svc.cluster.local
_npgsqlConnectionStringBuilder.Port = 5432
_npgsqlConnectionStringBuilder.Database = db1
_npgsqlConnectionStringBuilder.Username = postgres
_npgsqlConnectionStringBuilder.Password = "randompassword"
A few settings from pgpool.conf
disable_load_balance_on_write = 'transaction'
load_balance_mode = 'on'
backend_clustering_mode = 'streaming_replication'
Upvotes: 1
Views: 124