illusion466 illusion466 - 3 months ago 88
C# Question

connect to Postgresql with SSL

I am attempting to connect to a postgresql database which uses SSL via my c# application. But I'm unable to work out what the correct connection string would be. Is anyone able to help?

NpgsqlConnection postgresConn;
public PostgreManager()
{
openConnection();
}

private void openConnection()
{
postgresConn = new NpgsqlConnection("Server=10.153.8.4;Port=5432;Database=au_wa_jpc;User Id=readonly;Password=myPass;");
postgresConn.Open();

}


Edit:

I have attempted to use
Ssl Mode=Require;
in the connection string, however it throws the following exception.

An unhandled exception of type 'System.IO.IOException' occurred in Npgsql.dll

Additional information: TlsClientStream.ClientAlertException: CertificateUnknown: Server certificate was not accepted. Chain status: A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider.

. The specified hostname was not present in the certificate.

Answer

As described in the documentation here and here, you'll have to use SSL Mode=Require;Trust Server Certificate=true in your connection string.