porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.42k stars 271 forks source link

Support SNI for neon.tech #705

Closed py660 closed 11 months ago

py660 commented 12 months ago

Im trying to use neonDB in a project, but the following error keeps bugging me and blocks progress:

Unhandled Rejection at: Promise Promise {
  <rejected> Error: Endpoint ID is not specified. Either please upgrade the postgres client library (libpq) for SNI support or pass the endpoint ID (first part of the domain name) as a parameter: '?options=endpoint%3D<endpoint-id>'. See more at https://neon.tech/sni

what can i do to fix? I've tried putting the endpoint thing in the parameter, and in the password.

porsager commented 11 months ago

Their dashboard actually has ready connection params for this libary if you choose node.js under connection details ;)

I do like a single url instead of their setup with many env vars, so you can also try replacing in this url with your details:

`postgres://${ PGUSER }:${ PGPASSWORD }@${ PGHOST }/${ PGDATABASE }?sslmode=require&options=project%3D${ ENDPOINT_ID }`

The full options setup they provide looks like this:

// Do not expose your Neon credentials to the browser
// .env
PGHOST=...
PGDATABASE=...
PGUSER=...
PGPASSWORD=...
ENDPOINT_ID=...

// app.js
const postgres = require('postgres');
require('dotenv').config();

let { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env;

const sql = postgres({
  host: PGHOST,
  database: PGDATABASE,
  username: PGUSER,
  password: PGPASSWORD,
  port: 5432,
  ssl: 'require',
  connection: {
    options: `project=${ENDPOINT_ID}`,
  },
});

async function getPgVersion() {
  const result = await sql`select version()`;
  console.log(result);
}

getPgVersion();
py660 commented 11 months ago

tysm!