examples: Error: connect ECONNREFUSED ::1:443 with PostgreSQL and Next.js locally?
I have this defined in .env locally:
DATABASE_URL=postgresql://localhost:5432/mydb
POSTGRES_URL=postgresql://localhost:5432/mydb
I then have this kysely config (where DB is from codgen-kysely):
import 'dotenv/config'
import { createKysely } from '@vercel/postgres-kysely'
import { DB } from 'kysely-codegen'
export const db = createKysely<DB>()
export { sql } from 'kysely'
I then have a seed file at seeds/start.ts:
yarn ts-node -P tsconfig.cli.json seeds/start
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { db } from '../configurations/kysely'
seed()
async function seed() {
db.insertInto('foo').values(...)
}
What I am getting though is:
$ yarn ts-node -P tsconfig.cli.json seeds/start
$ ./node_modules/.bin/ts-node -P tsconfig.cli.json seeds/start
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: connect ECONNREFUSED ::1:443
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 443
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Why is it trying to connect to port 443 when I specified 5432 in the .env? Do I need to open up port 443 locally? That seems weird. How do I get around this locally?
I was able to run the kysely migrations fine, but not the seeds now, using the @vercel/postgres-kysely package.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 16
- Comments: 26
While I was following the Next.js dashboard-app tutorial, I had a similar problem connecting DB locally, so I came here while searching. After doing this and that, I found a solution and want to share it for the other people like me. I connected to DB using pg library instead
@vercel/postgreslibrary and made sql template literal work using code snippet of sql-pg library.Here is a modified seed.js file snippet.
Don’t forget to require
Clientfrompg.Just to give my complete snippets that is worked like a charm, thanks @NeuroWhAI and @Tobbe for the reference! 🚀
Since I want to use my
.env, then I need to install dotenv as well:And this is my complete
seed.js:My local
.env(just do it for example for other folks):From my experience, chapter 6 was the most time consuming, like 10:2 comparing to other chapters of the course. I think it would benefit from adding a small hint there about how to do local pg connection. I have no doubts that a real portion of users do go with local db setup in the course.
VERCEL - YOU NEED TO CALL OUT THAT THIS DOES NOT RUN USING LOCALHOST FOR DEV SETUP. WASTED MY TIME NOW. 🤦
I also tried Getting Started with Vercel Postgres
which generates the following env values: POSTGRES_URL POSTGRES_PRISMA_URL POSTGRES_URL_NON_POOLING POSTGRES_USER POSTGRES_HOST POSTGRES_PASSWORD POSTGRES_DATABASE
so I think configuring your local env with the value above will help you successfully connect from localhost.
@i-naeem in case of
data.tsfile, put the below snippet on top of the file. Well, It’s not a good code, but it will work well in the tutorial.You can also import this
sqlfunction and use in the same way as tutorial.For the people that come from the NextJS Dashboard tutorial that want to use a PostgreSQL local container in a typescript project, here’s my take on replacing
@vercel/postgreswith thepglib in theapp/seed/route.tsfileThis replaces the
client.sqlnotation with theclient.query()method.I copied the code from here: https://github.com/vercel/storage/blob/main/packages/postgres/src/sql-template.ts And then I combined it with the example from here: https://node-postgres.com/guides/project-structure#example
And that gave me a
data.tsfile that looks like the code below. I could successfully use this to complete the tutorial against my own local DB.And that last exported function can now be used as a taged template literal function, just like the tutorial is using
sqlfrom the Vercel Postgres package.I got this working for Drizzle using a docker-compose setup and a tiny bit of extra config code described here: https://gal.hagever.com/posts/running-vercel-postgres-locally.
My
db.tsfile:So both postgres and the web socket proxy are running in a local container. It should also be possible to just have the web socket proxy in a docker container (or even both on your host machine) but I didn’t try that.
So many thanks @Schniz for that article!
I’m also facing this issue. Please provide an example of how to switch to a locally installed database when using this package.
Worked great for me (note : I changed the env variable being used to match with the naming from the tutorial) … here’s the
docker-compose.ymlI’m using … in case someone is wondering …And here’s my
.env:I tried to do what is said in the tutorial. The database is working correctly, however, when I try to use it in my nuxt 3 application I get the error
[uncaughtException] TypeError: g.getRandomValues is not a functionI’m using node 18.14.2
Hello, I meet exactly the same issue. According my investigation, it comes with
localhostbecause if I put a Vercel postgres URL in the env variable, it works without any issue. For the moment I do not know how to solve it…@NeuroWhAI Thank you so much for sharing that snippet! Only thing I changed was the connection parameters for creating the client to look like this:
I’ve been running into this. Vercel might need to document what to do for a local setup. I’ve been reading this: https://neon.tech/blog/serverless-driver-for-postgres
If you scroll down to the bottom it talks about how to set things up locally under the section “Please do try this at home.” But I’ve been wrestling with the setup long enough. Most of that documentation focuses on
@neondatabase/serverlessand not Vercel’s integration which is very specific.Setup w/ a local db is a common enough use case, that I think it’d be wise to lay this on Vercel’s shoulders to document better or provide an easy way to do it.
FWIW, I can’t move forward w/ this
@vercel/postgresbecause I need to work locally, so just usingpostgresuntil I can figure out how to get it working.