PostgreSQL node server connecting to local pg rather than cloud (digitalocean) - node.js

Hi I am trying to connect to Postgres on digital ocean with a node basic server, but every time I hit a api end point like localhost:3001 etc instead of trying to connect to the pg on DO I get connected or try to connect to pg locally.
The credentials and config params all point to the digital ocean pg so I am not sure why is it trying to connect to the local pg on my Mac. I have a M1 Mac.
Another question is do I need pg locally installed to connect to DO pg? I removed the pg locally uninstalling everything and tried to connect to DO pg but it won't work, I get an error:
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
The reason I am asking is that I have almost exact logic on a electron node app, with the same configs and params pointing to DO pg and I can connect and fetch data etc it works either with local pg installed or with pg uninstalled locally so I am really confused.
On the node server I am using node pg npm package, which works fine on my electron app without the need of a local pg installed.
Please let me know.

So the problem was the .env vars not being mounted properly on the data.js configuration, meaning they were not defined and so node pg would automatically try to connect to local pg.
After inserting dotenv package on the data.js it works.

Related

Node.js PGAdmin Google Cloud Connecttion

In [ProjectA] I have
A VM Instance running Node.js
A cloud PGAdmin Database.
I need help to connect the database from the Node Application. I am getting this error.
Error: connect ENOENT /[InstanceName]/.s.PGSQL.5432 at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ENOENT', code: 'ENOENT', syscall: 'connect', address: ‘/[InstanceName]/.s.PGSQL.5432'
Suggestions/help would really be appreciated. This is my connection in the node.
user: 'postgres',//host: ‘[Host-Name]',
socketPath: ['Host-name', //host:[ExternalIP],
database: [Database],
password: [Password],
As the database is in Cloud SQL in the same project what you can do to connect is to use the Cloud SQL Proxy and by that you will be able to connect as to a local database to your VM.
To install and run the cloud SQL proxy you can follow This, I also put the instructions in general here:
1) Get the SQL Proxy and make it executable. For this you need to run the following 2 line in the VM:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
chmod +x cloud_sql_proxy
2) Create a service Account with the role of CloudSQL Client as detailed here
3) Create a key for the service account and upload it to your VM
4) Start the SQL proxy by running this command:
./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:5432 \
-credential_file=<PATH_TO_KEY_FILE> &
5) Connect in your app as if the Database was in localhost

Error connecting to Mongo Atlas

I am working on a nodejs project and using mongoose module I want to connect to Mongo Atlas,
I used
mongoose.connect('mongodb+srv://<USER>:<PASSWORD>#varcv-okhmh.mongodb.net/<DATABAS_NAME>?retryWrites=true');
but I got an error:
failed to connect to server [varcv-shard-00-00-okhmh.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 35.158.192.211:27017]
I also try to connect via old connection string
mongoose.connect('mongodb://peter:<PASSWORD>#varcv-shard-00-00-okhmh.mongodb.net:27017,varcv-shard-00-01-okhmh.mongodb.net:27017,varcv-shard-00-02-okhmh.mongodb.net:27017/<DATABAS_NAME>?ssl=true&replicaSet=varcv-shard-0&authSource=admin&retryWrites=true');
But I got the same error.
Notes:
everything is working perfectly on my local machine the error is happens when I upload my code to Godaddy Linux Shared hosting.
I am using PM2 to run my server.
I am using Mongo 3.6.5
I am using mongoose 5.1.5

Deploy meteor app in Azure on Windows

Hello i am have meteor app and want deploy on Azure on Windows, i try it by Meteor Up but can't do it
***\project2>npm install -g mup
***\project2>mup init
Meteor Up: Production Quality Meteor Deployments
------------------------------------------------
Empty Project Initialized!
***\project2>mup setup
Meteor Up: Production Quality Meteor Deployments
------------------------------------------------
Started TaskList: Setup (linux) [23.101.74.119] - Installing Node.js events.js:85
throw er; // Unhandled 'error' event
^ Error: connect ETIMEDOUT
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1000:19)
this error in Command Prompt of window from my local system
Looking at the error description it might be a connectivity issue. If you could share your mup.json file we might be able to spot the issue there.
Did you create an endpoint on TCP port 80? Check you nodeVersion setting so it corresponds to you Meteor app. The ROOT_URL should be in the format http://[myappname].cloudapp.net.
Using mup logs -n 50 you might detect some clues.

Nodejs in Openshift MongoDB Error

This could probably be very stupid and naive question, but I have tried all my ways to find the solution, but I'm unable to solve. Hope anyone can solve my problem. Thanks in advance.
Recently, I deployed my nodejs application in Open Shift using Mongolab driver for the MongoDB. When I run the application using 'rhc app start -a nodejs', it gives me an ECONNREFUSED error in mongooose.
Error: connect ECONNREFUSED
at errnoException (net.js:905:11)
at Object.afterConnect [as oncomplete] (net.js:896:19)
But, when I SSH into the server, go to $OPENSHIFT_REPO_DIR directory and use npm start, the app works very fine.
I connect to the mongodb by defining the URI as shown
mongoose.connect(process.env.MONGOLAB_URI || 'mongodb://localhost/<dbname>');
I defined the MONGOLAB_URI as an environmental variable in the server.
There would probably be something that I'm missing. But, please bare with this naive.

Error: connect ECONNREFUSED when making https request on same server running nginx

Since a recent Plesk update on my server I been having issues with my nodejs apps making requests to api urls hosted on the same server and a bit lost as to whats causing it as they use to run fine. The api domain in question is still serving the site using apache but when my nodejs app makes a request to the site using https I get the error
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
however loading the same url in my browser loads fine.

Resources