18

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.

2 Answers 2

48

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

Sign up to request clarification or add additional context in comments.

2 Comments

I wouldn't use Trust Server Certificate=true in production. You should pass Root Certificate and SSL Mode=VerifyFull instead. Refer to npgsql.org/doc/security.html#encryption-ssltls
@Miki If you have to be concerned about man-in-the-middle attacks, definitely.
0

What worked for me was reading this article.

PostgreSQL (SQL Authentication):

Server=127.0.0.1;Port=5432;Database=myDataBase;User ID=myUsername;Password=myPassword; SSL=TrueOrFalse;SslMode=RequireORDisable;

Server=IP Address;Port=5432;Database=myDataBase;Uid=myUsername;Pwd=myPassword; SSL=TrueOrFalse;SslMode=RequireORDisable;

The accepted answer did not work for me! It was rather this what worked:

SSL=TrueOrFalse;SslMode=RequireORDisable;

The following connection string is working for me - not in a C# project though:

"postgresql://<USER>:<PASSWORD>@$<POSTGRES_HOST>/<POSTGRES_DATABASE>?ssl=true&sslmode=require"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.