1

No matter what I do, I keep getting this error when running knex.

"errno": "EAI_AGAIN",
"code": "EAI_AGAIN",
"syscall": "getaddrinfo",
"hostname": "knex",
"host": "knex",
"port": 3

I'm connected to localhost and I'm sure I'm doing everything right. This exact setup worked when I last opened this project. I even confirmed I was on the right npm and node versions

const knexConnection = knex({
    client: 'pg',
    connection: "postgres://user:password@localhost:5432/db-name",
})

Putting the username, password, etc. into dbeaver works fine. So it's not an issue with the local psql server, nor is it an issue with the password and whatnot. Looking at the knex documentation too, this doesn't seem like a syntax issue.

What's going on? How can I fix this?

3 Answers3

1

From the error you can see that you are trying to look for hostname called knex and DNS times out. Check your wirings. Something is not configured the way you think it is.

Also check the stacktrace where exactly that error is coming from.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
0

When Mikael said that the host trying to be connected to was "knex" and not "localhost" I tried putting the variable in as an object.

const knexConnection = knex({
    client: 'pg',
    connection: {host:host, user:user, password:password, database:database, port:port},
})

and with the same values, it worked. So, I'm not sure why the URL one didn't work. Hopefully it was just this mishap, and it won't happen with my actual postgresql server.

0

In my humble opinion, given every other setting is ok, your password/user probably contains some special characters which broke the URL parser and then knex is trying to connect to some URL that does not exist.

You should try with another user, or changing user password to something without special characters. You can see this related question

This would explain even why your connection using a config object is working, but not with URI.

leonardfactory
  • 3,353
  • 1
  • 18
  • 25
  • perhaps that's what it was. I did just change the password and local psql servers due to rebooting linux. Regardless, putting it into an object with the exact same variables (imported from a .env) fixed the issue. – Andrew Jessen-Tyler Apr 28 '20 at 17:20