PostgresNIO Swift SNI Support

Hi there, I’m new here and I’d say I love the platform!

I wanted to implement Neon in a Vapor backend, but I get a connection is insecure (try using sslmode=require)error.
I read that PostgresNIO does not support SNI. I’m here to ask if there is any way to make it work.

Thanks in advance,
GP

Hey @gianpispi, welcome! :wave:

Have you tried the potential workarounds mentioned in the “connecting from older clients” docs?

Hi @Mahmoud, thanks! :blush:

I tried them all with no luck, unfortunately. Is there no other way?

I tested using Neon with PostgresNIO, and got it to work. It supports SNI out of the box, so the workarounds mentioned in the docs are not necessary. My code looks like this:

    let sslContext = try! NIOSSLContext(configuration: .makeClientConfiguration())

    // postgres://hlinnaka:<password-redacted>@ep-dry-breeze-785798.eu-central-1.aws.neon.tech/neondb
    let config = PostgresConnection.Configuration(
       connection: .init(
         host: "ep-dry-breeze-785798.eu-central-1.aws.neon.tech",
         port: 5432
       ),
       authentication: .init(
         username: "hlinnaka",
         database: "neondb",
         password: "<password-redacted>"
       ),
       tls: .require(sslContext)
    )

This is pretty much straight from the sample code in postgres-nio README. However, you need to enable tls.

@gianpispi, if you still have trouble connecting with this, please share your code

1 Like

Thank you so much @heikki! that works :blush:

1 Like