neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
343 stars 13 forks source link

Error: The server does not support SSL connections #27

Closed mattiaz9 closed 1 year ago

mattiaz9 commented 1 year ago

Steps to reproduce

This is an example route using next.js

import { Pool } from "@neondatabase/serverless"
import { sql } from "drizzle-orm"
import { drizzle } from "drizzle-orm/neon-serverless"

import { env } from "@/env.mjs"

export const runtime = "edge" // "nodejs"

export async function GET() {
  const db = drizzle(new Pool({ connectionString: env.DATABASE_URL }))
  const result = await db.execute(sql`SELECT now()`)
  return new Response(JSON.stringify(result))
}

Expected result

The executed query result

Actual result

Error: The server does not support SSL connections

Environment

macOS Ventura 13.4 edge runtime / nodejs 18.6 next.js 13.4.7

Logs, links

SSL is enabled for both Postgres (e.g. ?sslmode=require in the connection string) and the WebSocket tunnel (useSecureWebSocket = true). Double encryption will increase latency and CPU usage. It may be appropriate to disable SSL on the Postgres connection.

Other

I tried disabling SSL:

...
neonConfig.useSecureWebSocket = false
const db = drizzle(new Pool({ connectionString: env.DATABASE_URL, ssl: false }))
...

but now the request to neon db is stuck endlessly.

I also tried not to use drizzle, like in this example: https://neon.tech/docs/serverless/serverless-driver#neon-serverless-driver-with-vercel-edge-functions but the result is the same.

jawj commented 1 year ago

Thanks for this report. Are you using the latest driver version, 0.4.20? We addressed an issue rather like this recently.

mattiaz9 commented 1 year ago

Yes, 0.4.20 fixed the SSL issue, but i noticed that setting neonConfig.useSecureWebSocket = false still cause the request to stuck endlessly.

jawj commented 1 year ago

(If you are using the latest version, try also not changing any neonConfig settings with that).

mattiaz9 commented 1 year ago

setting neonConfig.poolQueryViaFetch = true is the only way to make it work, but i guess i doesn't use web sockets. it it any better in term of performance?

jawj commented 1 year ago

The driver options are provided mainly for testing, and for people using their own Postgres instance with a WebSocket proxy. Neon databases are not expected to work when neonConfig.useSecureWebSocket is false. I'm currently rewriting the README to reduce the prominence given to these options.

neonConfig.poolQueryViaFetch should indeed be a bit quicker, and will likely default to true in an upcoming version (it's false now while we do a bit of additional testing). If it's working well for you: great. As you suggest, it doesn't use WebSockets, so the useSecureWebSocket option is irrelevant here.