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:
Error:
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•2y ago
This very much looks like a .env issue when you look at your .env file.
does the url look like
xenial-blackOP•2y ago
Yeah, but it is quoted
adverse-sapphire•2y ago
that shouldn't do any issue
did you console log
process.env.POSTGRES_URL to see if it is set correctly?xenial-blackOP•2y ago
It returns undefined
adverse-sapphire•2y ago
are you using the normal nodejs runtime
xenial-blackOP•2y ago
Maybe that's it. It works on production though
adverse-sapphire•2y ago
if so it doesn't load env files automatically
xenial-blackOP•2y ago
Yeah i'm using normal runtime
adverse-sapphire•2y ago
are you maybe using the dotenv package in prod to inject it?
node -r dotenv/config yourFile.js should do the trickxenial-blackOP•2y 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•2y 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•2y 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-blackOP•2y ago
Yeah, I've never done backend before. I'm quite new to this stuff. Thank you for helping!
adverse-sapphire•2y ago
No problem, did the dotenv command fix it?
xenial-blackOP•2y ago
Yes it worked. I added the npm package