Search code examples
javascriptnode.jspostgresqlnpmknex.js

Node/Postgresql Error: connect ECONNREFUSED 127.0.0.1:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)


I've been running a Node server that connects to a Postgresql DB (via Knex). Everything was working absolutely fine and as intended until my laptop crashed. Since restarting, the DB connection will not work at all, and I get the following error:

Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)

Attempting a Postgresql connection via DBeaver also results in the same error. My server itself can start up absolutely fine. I only get the error when making an API call that tries to connect to the DB. My DB connection set up looks like this:

const db = knex({
  client: 'pg',
  connection: DATABASE_URL,
});

Again, this was all working perfectly until my laptop crashed. I'm guessing something happened during that process, but I'm not sure what or how to fix it. Any ideas/advice would be appreciated!

Full npm log below:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/Users/<name>/.nvm/versions/node/v12.16.2/bin/node',
1 verbose cli   '/Users/<name>/.nvm/versions/node/v12.16.2/bin/npm',
1 verbose cli   'run',
1 verbose cli   'dev'
1 verbose cli ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'predev', 'dev', 'postdev' ]
5 info lifecycle [email protected]~predev: [email protected]
6 info lifecycle [email protected]~dev: [email protected]
7 verbose lifecycle [email protected]~dev: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~dev: PATH:         /Users/<name>/.nvm/versions/node/v12.16.2/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/<name>/Desktop/BLOC/productify-server/node_modules/.bin:/Users/<name>/.nvm/versions/node/v12.16.2/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/<name>/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/<name>/Downloads/apache-maven-3.6.1 :/usr/local/share/dotnet
9 verbose lifecycle [email protected]~dev: CWD: /Users/<name>/Desktop/BLOC/productify-server
10 silly lifecycle [email protected]~dev: Args: [ '-c', 'nodemon src/server.js' ]
11 silly lifecycle [email protected]~dev: Returned: code: 130  signal: null
12 info lifecycle [email protected]~dev: Failed to exec dev script
13 verbose stack Error: [email protected] dev: `nodemon src/server.js`
13 verbose stack Exit status 130
13 verbose stack     at EventEmitter.<anonymous> (/Users/<name>/.nvm/versions/node/v12.16.2/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:310:20)
13 verbose stack     at ChildProcess.<anonymous> (/Users/<name>/.nvm/versions/node/v12.16.2/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:310:20)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid [email protected]
15 verbose cwd /Users/<name>/Desktop/BLOC/productify-server
16 verbose Darwin 19.4.0
17 verbose argv "/Users/<name>/.nvm/versions/node/v12.16.2/bin/node" "/Users/<name>/.nvm/versions/node/v12.16.2/bin/npm" "run" "dev"
18 verbose node v12.16.2
19 verbose npm  v6.14.4
20 error code ELIFECYCLE
21 error errno 130
22 error [email protected] dev: `nodemon src/server.js`
22 error Exit status 130
23 error Failed at the [email protected] dev script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 130, true ]

Solution

  • You are having a problem with your PostgreSQL in your Windows machine, try to restart your PostgreSQL server using the following command:

    pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" restart
    

    If this does not solve your problem, try to look at the logs where the problem is.

    The logs are available on the dir %PROGRAMFILES%\PostgreSQL\9.3\data\pg_log (adjusting for your PostgreSQL version and where you installed PostgreSQL if you didn't use the defaults).