storage: Trying to use a local DB with Vercel Postgres fails due to strict pool URL check
I thought I could use Vercel Postgres with a local DB for offline development with a custom connection string that points to a local DB…
import { createPool, sql } from '@vercel/postgres';
import { drizzle } from 'drizzle-orm/vercel-postgres';
export const db = drizzle(
process.env.NODE_ENV === 'production'
? sql
: createPool({
connectionString: process.env.POSTGRES_URL,
}),
{ logger: true }
);
…but this doesn’t work:
[VercelPostgresError]: VercelPostgresError - 'invalid_connection_string': This connection string is meant to be used with a direct connection. Make sure to use a pooled connection string or try `createClient()` instead.
The initial issue seems to be that the error is triggered if you don’t provide a pooled URL, and that check is hardcoded for the presence of -pooler. in the connection string, due to the URLs Vercel uses:
export function isPooledConnectionString(connectionString: string): boolean {
return connectionString.includes('-pooler.');
}
If I try and bypass this with a local URL like 'postgresql://jschuur:@localhost:5432/learnchineseclub?foo=-pooler.' then I get a new error:
The database host is 'localhost', which is the default host when none is set. If that's intentional, please ignore this warning. If not, perhaps an environment variable has not been set, or has not been passed to the library?
- error uncaughtException: Error: connect ECONNREFUSED ::1:443
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
digest: undefined
}
Is it currently possible to do local development without using a cloud hosted Vercel Postgres DB in both locations this way? Considering the 1 database limit on the free tier (and low other limits), this makes development rather difficult.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 12
- Comments: 17 (1 by maintainers)
I feel relying on the solution in https://gal.hagever.com/posts/running-vercel-postgres-locally is bound to introduce issues… is there a plan\possibility for better localhost support?
seriously? just to get postgres running locally?
This fix does not apply to Drizzle using @vercel/postgres
Perhaps it would be better to use
pgdirectly instead of@neondatabase/serverlesswhen connecting to a local database. As I understand it, the WebSocket connection is only required for compatibility with edge runtimes, and setting up a local WebSocket proxy seems unnecessarily complicated.For everyone here, I made it work using only
@vercel/postgres. I’ve done the following:.env.localdb:migrate:devthat basically run a script to generate tables and db columns.and let’s say you have
scripts/migrate.ts, In my code I make sure after i runcreateClient, I overrideSocketconfig.Currently, I’m still figuring out how
sqlwould run locally as well.I believe this is the blog post that was mentioned earlier https://gal.hagever.com/posts/running-vercel-postgres-locally
For my Drizzle specific use case, I ended up just initialising based on
NODE_ENVquick and dirty like this:No doubt this will affect my bundle size, but the import costs don’t seem to be too high.
I had to use environment specific methods for
migratein the code above, but Drizzle’ssqlseemed to work in both environments when I needed to add randomisations e.g., so it looks like it might turn into a bit more overhead as my current project grows:@webhype assuming you already have a local postgres installation, just run wsproxy without docker:
The localhost PR was merged and there’s a blogpost explaining how to use it: https://gal.hagever.com/posts/running-vercel-postgres-locally
Open a new issue if you’re still having problems with localhost postgres, thanks!
@Schniz
The localhost PR has merged – I know you were writing a blog post about how to set all of that up. Can you share it here and close the issue when you do?
<picture data-single-emoji=":thanku:" title=":thanku:">
</picture>
There’s currently a PR open (https://github.com/vercel/storage/pull/118) for this! However, there’s some additional setup required even after that PR is merged – Neon’s client (the client we use internally) uses websockets instead of regular tcp sockets, so a websocket proxy is required (here’s theirs: https://github.com/neondatabase/wsproxy).
I’m not sure how to set it up (haven’t needed to), but I know @Schniz has, so maybe he can help out.