Connection to external postgres database times out - node.js

I am trying to connect a client device to a PostGreSQL host device. I have 2 devices with PostGreSQL on them: A Windows OS device (Host) and a RaspberryPi running raspbian (Client).
I have pgAdmin on the host device, with a PostGreSQL server running on it (The server is the default PostGreSQL server with default configurations).
The host device and the client device are on the same network (wifi).
I have the following node.js code on the client device:
const { Client } = require('pg');
const connectionString = 'postgres://postgres:MyPassword#192.168.1.8:5433/postgres';
const client = new Client({
connectionString: connectionString
});
client.connect();
client.query('CREATE TABLE test (something text, number int)', function(err,result){
if (err) throw err;
console.log(result);
console.log('hi');
});
based on the code above, the client would connect and query the making of a table, then return the result and 'hi' in the command line.
When I run it, it does nothing. After a minute or so, the connection times out.
Is there anything that I'm missing?
I used Wireshark to see if the client device was even sending packets. The client device was sending packets (directed to the postgresql post), so my assumption is that the problem is with the host device, but I don't know why because I have already edited the configuration files multiple times by now.
I have already...
changed config files
restarted the system
re-installed the software
switched to trying to connect from psql
Well, this is interesting...
So I found the real .config files on the host device, and I changed listen_addresses = '*' to listen_addresses = '192.168.1.12'.
After I made those changes, I tried to restart postgres and then the service stopped, but refused to be started again. I opened pgAdmin 4, and none of the servers were wanting to connect. After I changed the file back to listen_addresses = '*' the service started and everything was back to normal. My conclusion now is that there is a problem with the firewall. Does anyone here do Windows and can help me configure the firewall??

Turns out, I was bothering myself with the Postgres configuration files, but I had to look at the firewall configurations. Those were the ones that were blocking the pi. If anyone has the same problem, I suggest looking at your firewall first. (Specifically allowing port 5432)

Related

Server Port over from Mac OS to Ubuntu does not work (Express + ReactJS)

I have the exact copy and setting ported to Ubuntu and for some reason I get a
'Failed to load resource: net::ERR_EMPTY_RESPONCE' via chrome DevTools.
The express server is running with PM2 and I have mongoDB running as a service. The goal was to have the server run and connect on reboot (for a store POS system).
On my Mac, it runs connect and such forth no problem.
On my Ubuntu box, the React app does launch, I just cannot seem to connect to the database. The db setting are the exact same (ports and all) from my mac to ubuntu.
I used MongoDB Compass on the ubuntu system to view the database once the service is started and it does connect, and I see all of the migrated data. When I use postman to try to get a response from my server, it just hangs and nothing happens. I'm not even sure how to diagnostic this issue.
Please inform me what information you need and I'll happily forward it.
The issue was that on my mac, I didn't have a process.env.MONGOLAB_URI setup, so it went to the testDB. But, it seems that on my Ubuntu system there is a default process.env that got created or is used.
var mongoUri = process.env.MONGOLAB_URI || 'mongodb://localhost/testDB'
mongoose.connect(mongoUri);
mongoose.Promise = global.Promise;
simply change this line to this
var mongoUri ='mongodb://localhost/testDB'
allowed it to work. Future note on migration if anyone runs into this problem!

Run a VPN client on a remote website

I have been researching this for day and I haven't been able to find the way to do this.
I am building a react app, running express at the backend, that needs to access some data in a remote database that lives inside a VPN. At the moment the app lives on my localhost so its enough for me to connect my machine using openvpn client and everything works a beauty. The problem will rise when the app will be live and I will need it to have access to the vpn by (I'm guessing) having a vpn client running on the site/domain.
Has anyone done this before?
I have tried to install the node-openvpn package that seems could do the job but unfortunately I can't manage to make it work as the connection doesn't seem to be configured properly.
This is the function I call to connect to the vpn that systematically fails at the line
--> openvpnmanager.authorize(auth);
const openvpnmanager = require('node-openvpn');
...
const connectToVpn = () => {
var opts = {
host: 'wopr.remotedbserver.com',
port: 1337, //port openvpn management console
timeout: 1500, //timeout for connection - optional,
logpath: '/log.txt'
};
var auth = {
user: 'userName',
pass: 'passWord',
};
var openvpn = openvpnmanager.connect(opts);
openvpn.on('connected', function() {
console.log('connecting..');
openvpnmanager.authorize(auth); <-- Error: Unhandled "error" event. (Cannot connect)
});
openvpn.on('console-output', function(output) {
console.log(output)
});
openvpn.on('state-change', function(state) { //emits console output of openvpn state as a array
console.log(output)
});
};
Am I misusing this function? Is there a better way?
Any help will be extremely appreciated.
Thank You!
The problem will rise when the app will be live and I will need it to
have access to the vpn by (I'm guessing) having a OpenVPN client running
on the site/domain.
Thats correct, you will need an openvpn client instance on the server where you will run the backend.
The above library (node-openvpn) is simply a library to interact with the local OpenVPN client instance. It cannot create a connection on its own. It depends on the OpenVPN binary (which should be running).
The solution you need is simply run the OpenVPN client on your server (apt-get openvpn). And let the daemon run. Check out the references below.
node-openvpn issues that points out that a running instance of the client is needed
OpenVPN CLI tutorial

Socket connection not recognising all clients/connections

So far sockets is working as expected. On my server I have:
const io = require('socket.io')(1923);
// after an action
io.sockets.emit('items', data);
Then on the client side:
const socket = io.connect('http://localhost:1923');
socket.on('items', (items) => {
// do stuff with items
});
Now this is working nicely when I'm on my own machine. As soon as different devices open the site it only updates on the browsers on my computer.
Computer
Firefox has the site open
Chrome too
Mobile
Site is open on Chrome for mobile
Now after an action takes place, only the sites on the computer gets updated and not my new device. Checking the connections it also only recognises clientsCount: 2. (instead of 3)
The site is open on three separate browsers. What am I doing wrong for this to be happening?
(sidenote: I've tried the same with a second computer, it's not specific to the mobile device.)
Making my comment into an answer since it seemed to have solved your issue...
This client code:
const socket = io.connect('http://localhost:1923');
won't work anywhere except on the same computer as the server is on. localhost is, by definition`, the local computer and only the local computer. You will need to connect by IP address or by DNS name.
Perhaps you want this:
const socket = io.connect('http://192.168.0.10:1923');

NodeJs text file writing

I am trying to write a text file from NodeJs. I have server running on my laptop. Currently, i am running client on my laptop and it works fine. But if i run same NodeJs client on Linux running on raspberrypi, it doesn't write on file or neither it gives any error.
I have the following code for client
var ioC = require('socket.io-client'),
ioClient = ioC.connect('http://localhost:4000'),
fs = require('fs'),
os = require('os');
ioClient.on('connect', function () { console.log("socket connected"); });
ioClient.on('ChangeState', function(msg){
console.log(msg);
fs.writeFile('server.txt', JSON.stringify(msg), function (err){
if (err) return console.log(err);
});
});
Can anybody please help me what can be the issue with this?
You are connecting to localhost which won't work if the client is on a different machine. You need to change the server to listen to the ip address your server has in your network, and also need to let your client connect to this ip. You can get the ip by running ifconfig in your terminal. Then (depending on wireless or wired connection) look for something like (usually the last paragraph):
and create the server on this ip. E.g.
192.168.178.30:4000
and connect to the same address from your client.
To find your ip on windows, refer to this guide

Can't access MongoDB from subdomain on same server

I am running NginX, Node and Mongodb. And it seems that I can't acces the same database from a second app I am running. For example, I don't get anything back when I do:
collection.findOne({
name: someName
}, function(err, results){
// Returns no errors or results. Just stops working.
});
I can access the database perfectly fine from my first app, but not the second one.
This is the code I use to connect to the database in both apps.
Server = require('mongodb').Server,
Db = require('mongodb').Db,
db = new Db('database', new Server('localhost', 27017, { auto_reconnect: true }), { w: true });
Anyone know what the problem might be?
Edit: Does it have something to do with the subdomain or ports? Too many connections?
Edit 2 (more info):
I run mongodb with service mongodb start.
In my /etc/mongodb.conf I have bind_ip = 127.0.0.1 and dbpath=/var/lib/mongodb (rest is default)
In both my apps I run the same code to establish a connection to the database, but only the first one works (I know that because I am able to retrieve information from the database in my first app).
The apps are running on different ports. The first one is running on port 1337 and the second one runs on 3000.
You are using 'localhost' as the host name to connect to this server.
This means you will only be able to connect from the same machine that mongod is running on with that hostname.
Unless all your apps run on the same server as mongod you will need to change your connect code to use the actual hostname of the mongod server.

Resources