illusion466 illusion466 - 1 year ago 500
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()

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



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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download