NeonN
Neon2y ago
17 replies
endless-jade

Cannot connect to Neon database from local machine (Node)

Hi, I want to interact with my Neon database from my machine (that will make development easier)
I'm using Node + Express as an API that communicates with Neon.

I can connect to the database with pgadmin and psql from my machine.
The deployed version of the API can connect to Neon as well, but I can't connect from local Node.
The environment variables are correct.

Code:
import pg from 'pg'
import express from 'express'

const app = express()
const port = process.env.PORT

const { Pool } = pg
const pool = new Pool({
    connectionString: process.env.POSTGRES_URL
})

pool.connect()

app.get('/', (req, res) => {
    res.send("Hello from Express!")
})

app.listen(port, '0.0.0.0')

Error:
Error: connect ECONNREFUSED 127.0.0.1:5432
    at /home/username/Projects/project-name/backend/node_modules/pg-pool/index.js:45:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 5432
}

Node.js v18.19.0
[nodemon] app crashed - waiting for file changes before starting...

It looks like it's trying to connect to 127.0.0.1 (localhost), even though it should be connecting to Neon?
Was this page helpful?