N
Neon2y ago
eastern-cyan

Issue with neondatabase/wsproxy for local development.

Hi, I'm running into this error using the neon http driver with the docker image. I followed this guide: https://gal.hagever.com/posts/running-vercel-postgres-locally NeonDbError: Error connecting to database: Failed to parse URL from https://api.0.0.0/sql Here is the code
import { neon, Pool, neonConfig } from '@neondatabase/serverless';
import ws from 'ws';

neonConfig.webSocketConstructor = ws;
if (!process.env.VERCEL_ENV) {
neonConfig.wsProxy = (host) => `${host}:5433/v1`;
neonConfig.useSecureWebSocket = false;
neonConfig.pipelineTLS = false;
neonConfig.pipelineConnect = false;
neonConfig.forceDisablePgSSL = true;
}

// const pool = new Pool({ connectionString: "postgres://postgres:postgres@0.0.0.0:5433/postgres" });
const sql = neon("postgres://postgres:postgres@0.0.0.0:5433/postgres");


async function main() {
const result = await sql`SELECT 1 + 1 AS solution`;
// const result = await pool.query(`SELECT 1 + 1 AS solution`);
console.log(result);
process.exit(0)
}

main()
import { neon, Pool, neonConfig } from '@neondatabase/serverless';
import ws from 'ws';

neonConfig.webSocketConstructor = ws;
if (!process.env.VERCEL_ENV) {
neonConfig.wsProxy = (host) => `${host}:5433/v1`;
neonConfig.useSecureWebSocket = false;
neonConfig.pipelineTLS = false;
neonConfig.pipelineConnect = false;
neonConfig.forceDisablePgSSL = true;
}

// const pool = new Pool({ connectionString: "postgres://postgres:postgres@0.0.0.0:5433/postgres" });
const sql = neon("postgres://postgres:postgres@0.0.0.0:5433/postgres");


async function main() {
const result = await sql`SELECT 1 + 1 AS solution`;
// const result = await pool.query(`SELECT 1 + 1 AS solution`);
console.log(result);
process.exit(0)
}

main()
9 Replies
eastern-cyan
eastern-cyanOP2y ago
The Pool way works perfectly I think the error comes from the @neondatabase/serverless, because if i change the host to postgres://postgres:postgres@1.2.3.4:5433/postgres, then the error becomes NeonDbError: Error connecting to database: Failed to parse URL from https://api.2.3.4/sql
blank-aquamarine
blank-aquamarine2y ago
I believe "https://api.0.0.0/sql" isn't a valid URL. Is that the real value you're using?
node -e "new URL('https://api.0.0.0/sql')"
node -e "new URL('https://api.0.0.0/sql')"
This throws an error for me
The Pool way works perfectly
This is interesting. That does suggest something odd with the hostname parsing for HTTP queries. Do you mind sharing your hostname? You can DM me if you prefer
eastern-cyan
eastern-cyanOP2y ago
Oh I'm not using Neon Cloud for this test. https://gal.hagever.com/posts/running-vercel-postgres-locally
Running Vercel Postgres Locally
The setup I use for local work with Vercel Postgres or Neon database
eastern-cyan
eastern-cyanOP2y ago
It works great with the neon connection string, i'm trying to make it run locally to avoid using a branch for development I'm using this docker compose file
services:
postgres:
image: "postgres:latest"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- "5432:5432"
pg_proxy:
image: ghcr.io/neondatabase/wsproxy:latest
environment:
APPEND_PORT: "postgres:5432"
ALLOW_ADDR_REGEX: ".*"
LOG_TRAFFIC: "true"
ports:
- "5433:80"
- "2112:2112"
depends_on:
- postgres
services:
postgres:
image: "postgres:latest"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- "5432:5432"
pg_proxy:
image: ghcr.io/neondatabase/wsproxy:latest
environment:
APPEND_PORT: "postgres:5432"
ALLOW_ADDR_REGEX: ".*"
LOG_TRAFFIC: "true"
ports:
- "5433:80"
- "2112:2112"
depends_on:
- postgres
blank-aquamarine
blank-aquamarine2y ago
Sorry for the delay, I took a quick look at this code and ran it locally. Then it hit me 🙂 You're running the ws-proxy, that's why pool works. Pool works over websockets. You need the HTTP proxy. George has shared some details here: https://github.com/neondatabase/serverless/issues/33#issuecomment-1634853042
GitHub
Any way to connect from localhost? · Issue #33 · neondatabase/serve...
I would like to use an offline local instance of postgress while developing on my computer, I'm trying something like this: import { neon, neonConfig } from '@neondatabase/serverless' i...
blank-aquamarine
blank-aquamarine2y ago
Someone has commented on that issue and shared a container with the setup. I will look into this sometime during the week.
eastern-cyan
eastern-cyanOP2y ago
Thanks for the help, on that issue is this repo https://github.com/TimoWilhelm/local-neon-http-proxy that seems to solve the issue
GitHub
GitHub - TimoWilhelm/local-neon-http-proxy: Connect to a local Post...
Connect to a local PostgreSQL database using the neon http proxy - TimoWilhelm/local-neon-http-proxy
eastern-cyan
eastern-cyanOP2y ago
i will give it a try
stormy-gold
stormy-gold2y ago
We're going to be working on a docker build for postgres + http support soon, but it got bumped for higher priority things atm

Did you find this page helpful?