Skip to content

OTP 26 changes SSL defaults and your Postgres connection might break

OTP 26 changes the default SSL verify option. This can break your Ecto Postgres connection if you rely on SSL without supplying CA certificates.

September 29, 2023
2 min read

OTP 26, the Erlang release that Elixir apps run on, ships with safer SSL defaults. The default for the verify option changed from :verify_none to :verify_peer. So host verification is now on by default, and it needs trusted CA certificates, supplied via cacerts or cacertsfile.

It’s a good change for security, but it can catch you off guard when upgrading.

The problem

If you connect to Postgres over SSL (mandatory on providers like Digital Ocean), the connection fails after upgrading to OTP 26. The error looks like this:

** (DBConnection.ConnectionError) connection not available and request was dropped
from queue after 15000ms. This means requests are coming in and there are no idle
connections, no ## connection processes are available to be started and/or checked out,
or the pool is otherwise not accepting requests

The underlying issue is that Postgrex passes your ssl_opts to Erlang’s :ssl module, and that module now requires CA certificates for peer verification.

The fix

You could set verify: :verify_none to get the old behaviour back, but that turns off certificate verification entirely. Since OTP 25, :public_key.cacerts_get/0 can fetch the CA certificates from the operating system’s certificate store instead. It works on Linux, macOS and Windows, without any hardcoded file paths. Add this to your runtime.exs:

config :myapp, MyApp.Repo,
  # ...
  ssl_opts: [
    verify: :verify_peer,
    cacerts: :public_key.cacerts_get()
  ]

It has to go in runtime.exs (not config.exs or prod.exs), because :public_key.cacerts_get/0 is a runtime call. It should read the certificates on the machine your app runs on, not on your build machine.

If you run in a Docker container, make sure the image has the ca-certificates package installed.

A note on Azure

This works with Azure Database for PostgreSQL without further changes. Azure uses certificates signed by publicly trusted root CAs (DigiCert Global Root G2, Microsoft RSA Root CA 2017), and standard OS certificate stores already include those.

If you connect through Azure Private Endpoints with custom DNS, hostname verification might fail, because the certificate’s common name doesn’t match your private DNS name. In that case, set server_name_indication explicitly:

ssl_opts: [
  verify: :verify_peer,
  cacerts: :public_key.cacerts_get(),
  server_name_indication: ~c"your-server.postgres.database.azure.com"
]

—

Back to Blog

Next: Integrate IBM Plex with TailwindCSS and autogenerate font rules

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace