N
Neon2y ago
xenial-black

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')
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...
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?
15 Replies
adverse-sapphire
adverse-sapphire2y ago
This very much looks like a .env issue when you look at your .env file. does the url look like
POSTGRES_URL=postgresql://neon_owner:somePassword@.....eu-central-1.aws.neon.tech/SomeDb?sslmode=require
POSTGRES_URL=postgresql://neon_owner:somePassword@.....eu-central-1.aws.neon.tech/SomeDb?sslmode=require
xenial-black
xenial-blackOP2y ago
Yeah, but it is quoted
adverse-sapphire
adverse-sapphire2y ago
that shouldn't do any issue did you console log process.env.POSTGRES_URL to see if it is set correctly?
xenial-black
xenial-blackOP2y ago
It returns undefined
adverse-sapphire
adverse-sapphire2y ago
are you using the normal nodejs runtime
xenial-black
xenial-blackOP2y ago
Maybe that's it. It works on production though
adverse-sapphire
adverse-sapphire2y ago
if so it doesn't load env files automatically
xenial-black
xenial-blackOP2y ago
Yeah i'm using normal runtime
adverse-sapphire
adverse-sapphire2y ago
are you maybe using the dotenv package in prod to inject it? node -r dotenv/config yourFile.js should do the trick
xenial-black
xenial-blackOP2y ago
It's the same in prod, i haven't configured anything related to environment variables. I just entered them in the platform's variables editor.
adverse-sapphire
adverse-sapphire2y ago
https://www.npmjs.com/package/dotenv dotenv loads env files for you
npm
dotenv
Loads environment variables from .env file. Latest version: 16.4.5, last published: 2 months ago. Start using dotenv in your project by running npm i dotenv. There are 42370 other projects in the npm registry using dotenv.
adverse-sapphire
adverse-sapphire2y ago
ah yeah that would explain it if you host it somewhere that handles environment variables for you there is no need to do anything. But when you code locally you will need to handle this yourself
xenial-black
xenial-blackOP2y ago
Yeah, I've never done backend before. I'm quite new to this stuff. Thank you for helping!
adverse-sapphire
adverse-sapphire2y ago
No problem, did the dotenv command fix it?
xenial-black
xenial-blackOP2y ago
Yes it worked. I added the npm package

Did you find this page helpful?