No Username specified in startup packet - node.js

I am opening up a project from a year and a half ago and my local PostgreSQL database is not working. I am using a react front end, with a Node.js back end. The Node module I am using to connect with the local database is pg.pool. The error I get when I submit the data through pg.pool is:
Error with query for user error: no PostgreSQL user name specified in startup packet
I was reading on the internet and apparently the client has to submit a username to the backend for the Database to open up. The internet suggested opening the pg_hba.conf file and checking that the users are correct, but I checked the file and the users for the PostgreSQL is set to all, so I don't think that is the problem. Here is that file:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all scram-sha-256
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
Here is my pool.js file that connects the node server to the postgresql instance running locally, it uses a if else statement incase I want to connect locally or in case I want to connect to heroku:
const pg = require('pg');
const url = require('url');
let config = {};
if (process.env.DATABASE_URL) {
// Heroku gives a url, not a connection object
// https://github.com/brianc/node-pg-pool
const params = url.parse(process.env.DATABASE_URL);
const auth = params.auth.split(':');
config = {
user: auth[0],
password: auth[1],
host: params.hostname,
port: params.port,
database: params.pathname.split('/')[1],
ssl: { rejectUnauthorized: false },
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};
} else {
config = {
host: 'localhost', // Server hosting the postgres database
port: 5432, // env var: PGPORT
database: "workout_app_prime", // CHANGE THIS LINE! env var: PGDATABASE, this is likely the one thing you need to change to get up and running
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};
}
// this creates the pool that will be shared by all other modules
const pool = new pg.Pool(config);
// the pool with emit an error on behalf of any idle clients
// it contains if a backend error or network partition happens
pool.on('error', (err) => {
console.log('Unexpected error on idle client', err);
process.exit(-1);
});
module.exports = pool;
Other relevant facts: yesterday morning working on a different project my computer crashed, PostgreSQL wasn't working when I tried to reopen that project because the project didn't terminate correctly, So I deleted the .pid file and restarted the computer then it started working.
When I made this database a year and a half ago I was running postgreSQL 12, now I am on 13.
I am running on Mac OS 10.15, idk if that is relevant.
Anybody have any ideas? Thanks!

Related

nodejs express webapp - ERROR: read ECONNRESET

When I leave my application idle for a few minutes, it errors out. This is my first webapp I created and it is sending and receiving data from a mysql database - somewhat of a sign up form. Is there a way to stop this from happening? Thanks!
var connection = mysql.createPool({
host: host,
user: user,
password: password,
database: db,
ssl: ssl
});
My original code was var connection = mysql.createConnection...
I replaced that and removed connection.connect();
This seems to happen when establish a single use connection, it can be avoided by establishing a connection pool instead.

MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms

I am trying to create a fullstack app reading the following tutorial:
https://medium.com/javascript-in-plain-english/full-stack-mongodb-react-node-js-express-js-in-one-simple-app-6cc8ed6de274
I followed all steps and then tried to run:
node server.js
But I got the following error:
MongoDB connection error: MongoTimeoutError: Server selection timed
out after 30000 ms
at Timeout._onTimeout (C:\RND\fullstack_app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:308:9)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) { name: 'MongoTimeoutError', reason: Error: connect ETIMEDOUT
99.80.11.208:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {} }, [Symbol(mongoErrorContextSymbol)]: {} } (node:42892)
UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection
timed out after 30000 ms
at Timeout._onTimeout (C:\RND\fullstack_app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:308:9)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
My code at server.js is as follows:
const mongoose = require('mongoose');
const router = express.Router();
// this is our MongoDB database
const dbRoute =
'mongodb+srv://user:<password>#cluster0-3zrv8.mongodb.net/test?retryWrites=true&w=majority';
mongoose.Promise = global.Promise;
// connects our back end code with the database
mongoose.connect(dbRoute,
{ useNewUrlParser: true,
useUnifiedTopology: true
});
let db = mongoose.connection;
db.once('open', () => console.log('connected to the database'));
Any suggestions?
just go to mongodb atlas admin panel.Go in security tab>Network Access> Then whitelist your IP by adding it
See this image to locate the particular menu
Note:Check your IP on google then add it
I wasted whole day on this because the whitelist suggestion was not my issue (I'm running in docker compose with --bind_ip option set properly and I can see the Connection accepted in the mongo logs each time my client tried to connection).
It turns out, I simply needed to add this to the end of my connection string in the code:
?directConnection=true
connection string:
mongodb://myUserId:myPassword#mongodb/myDatabase?directConnection=true
I hope mongodb documents this better because I only stumbled across it from looking at the mongosh logs when I connected using that client.
Sometimes this error occurs due to the IP Address given access to in your database.
Mind you, your application can be working well then come up with such an error.
Anytime this happens, most times your IP address has changed, which is not on the whitelist of addresses allowed. (IP addresses can be dynamic and are subject to change)
All you have to do is to update the whitelist with your current IP addresses
Sometimes it will also happen when your MongoDB services are turned OFF.
Here are the steps to Turn ON the MongoDB Services:
Press window key + R to open Run window.
Then type services.msc to open services window.
Then select MongoDB server, right-click on it, finally click on the start.
I got the same error. These are the steps I followed for resolve it
Log into your Mongo Atlas account
Go to the security tab -> Network Access -> Then whitelist your IP
Then go to the security tab -> Database Access -> and delete your current user.
Create a new user by providing new username and password.
Provide that newly created username and password in the .env file or mongoose.connect method
After these steps it worked as usual. hope this will help you guys.
Ensure that the "<" and ">" are removed when you replace the user and password fields. This worked for me
In my case, this issue happened after NodeJS version upgrade from Node 14 to Node 17 and the mongoose was not connecting to MongoDB in local.
Solution - In connection string, change localhost to 127.0.0.1.
Ref - https://github.com/Automattic/mongoose/issues/10917#issuecomment-957671662
Are you using any Antivirus, Firewall, VPN or on the restricted network (e.g. work/commercial Wi-Fi/LAN connection)? Then try to turn it off/reconnect to a different network. Some IPs/connections might be blocked by the administrator of a network that you're using or simply antivirus might have firewall policies. Even though if you have 0.0.0.0 in your IP Address at the MongoDB Atlas.
I am able to solve the issue. Everything was fine, but the firewall was blocking access to port 27017. connectivity can be tested at http://portquiz.net:27017/ or using telnet to the endpoint which can be retrieved from clusters->Metrics.
Thanks, everybody for the suggestions
Whitelist your connection IP address.
Atlas only allows client connections to the cluster from entries in the project’s whitelist. The project whitelist is distinct from the API whitelist, which restricts API access to specific IP or CIDR addresses.
NOTE
You can skip this step if Atlas indicates in the Setup Connection Security step that you have already configured a whitelist entry in your cluster. To manage the IP whitelist, see Add Entries to the Whitelist.
If the whitelist is empty, Atlas prompts you to add an IP address to the project’s whitelist. You can either:
Click Add Your Current IP Address to whitelist your current IP address.
Click Add a Different IP Address to add a single IP address or a CIDR-notated range of addresses.
For Atlas clusters deployed on Amazon Web Services (AWS) and using VPC Peering, you can add a Security Group associated with the peer VPC.
You can provide an optional description for the newly added IP address or CIDR range. Click Add IP Address to add the address to the whitelist.
You can remove { useUnifiedTopology: true } flag and reinstall mongoose dependecy!
it worked for me.
To anyone still struggling through this issue. I resolved this issue by setting all the parameters like username, password and dbName in mongoose options itself.
Earlier the code was like this. (When I was getting the error).
import mongoose from "mongoose";
mongoose.Promise = require("bluebird");
let dbName = process.env.DB_NAME;
const dbAddress = process.env.DB_HOST;
const dbPort = process.env.DB_PORT;
if(!dbName || !dbAddress || !dbPort){
throw new Error("Mongo error unable to configuredatabase");
}
let options = {
useNewUrlParser: true,
useUnifiedTopology: true,
user:process.env.DB_USER,
pass: process.env.DB_PASS
};
mongoose.connect(`mongodb://${dbAddress}:${dbPort}/${dbName}`, options).catch(err => {
if (err.message.indexOf("ECONNREFUSED") !== -1) {
console.error("Error: The server was not able to reach MongoDB. Maybe it's not running?");
process.exit(1);
} else {
throw err;
}
});
Note that url in mongo connect. mongodb://${dbAddress}:${dbPort}/${dbName}.
New code without error.
import mongoose from "mongoose";
mongoose.Promise = require("bluebird");
let dbName = process.env.DB_NAME;
const dbAddress = process.env.DB_HOST;
const dbPort = process.env.DB_PORT;
// console.log("process.env", process.env)
if(!dbName || !dbAddress || !dbPort){
throw new Error("Mongo error unable to configuredatabase");
}
let options = {
useNewUrlParser: true,
useUnifiedTopology: true,
user:process.env.DB_USER,
pass: process.env.DB_PASS,
dbName: dbName
};
mongoose.connect(`mongodb://${dbAddress}:${dbPort}`, options).catch(err => {
if (err.message.indexOf("ECONNREFUSED") !== -1) {
console.error("Error: The server was not able to reach MongoDB. Maybe it's not running?");
process.exit(1);
} else {
throw err;
}
});
Note the options and the url.
This is my local env. Not production env.
Hope It'll be helpful.
try to specify the node driver, version 2,2,12 in cluster --> connect --> connect your application. new string must be with mongodb://. use this string to connect. do not forget to enter a password
Most of the answers present in this thread should fix you're issue, I tried all the answers and whitelisted all the IP's but still I was not able to connect to the database.
Interestingly, my system was connected to a VPN while I was trying to connect to the mongo atlas. So, I just disconnected the VPN and was able to connect to the db. This was just an simple issue due to which I was not able to connect to the database.
i was has this problem, to me the solution was check if my ip be configured correctly and before confirm in this screen
enter image description here
Please Check your Password, Username OR IP configuration. Mostly "Server selection timed out after 30000 ms at Timeout._onTimeout" comes whenever your above things are not matched with your server configuration.
Try to make new User in Database Access with the default Authentication Method which is "SCRAM". Then make a new Cluster for that. It worked for me! My
server.js file
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
//const uri = process.env.ATLAS_URI;
mongoose.createConnection("mongodb+srv://u1:u1#cluster0-u3zl8.mongodb.net/test", { userNewParser: true, useCreateIndex: true, useUnifiedTopology: true}
);
const connection = mongoose.connection;
connection.once('once', () => {
console.log(`MongoDB databse connection established successfully`);
})
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
What is your mongoose version? because there is an issue with a certain mongoose version. Please visit this link for more details "https://github.com/Automattic/mongoose/issues/8180". Anyway, I was facing the same issue but after using an older version (5.5.2), it worked for me. Please give a try and let me know what happened.
It might basically be because of an npm update.
I just updated to npm v 6.13.7. and I began to get that error.
Following instructions from #Matheus, I commented the line "{ useUnifiedTopology: true }" and I was able to get through it.
I updated mongoose and everything works fine.
I had the same issue. I could connect from MongoDB Compass or my Node app using the connection strings Atlas gave me. I resolved it by adding my IP Address into the Whitelist in Atlas.
For anyone who may face this problem with mongoose v6+ and nodejs and white listing your ip has not solved it or changing from localhost to 127.0.0.1.
Solution
Set the following options for the mongoose client connection options:
mongoose.connect(url, {
maxIdleTimeMS: 80000,
serverSelectionTimeoutMS: 80000,
socketTimeoutMS: 0,
connectTimeoutMS: 0
}
These options stop the mongoose client from timining out the connection before it selects a primary from the DB cluster.
Please remember to set any other options relevant to your setup like if you are running a cluster, make sure to set the replicaSet option or if you have authentication enabled, set the authSource option or the database you are connecting to dbName.
Some of these options can be set in the connection string.
The full list of options can be found here: https://mongodb.github.io/node-mongodb-native/4.2/interfaces/MongoClientOptions.html#authSource
This must be a temporary network issue. Please try to connect after some time and hopefully it should be fine. but make sure you white list your ip address.
If your config is alright and still the error shows up, it maybe because of too much data in the db (Like for me, I am using the free tier for testing my app where I upload posts and the posts are images with some description), so I just deleted the db and it worked fine after that. Hope this helps someone.
I have solved this problem by using the following way, use a callback.
const uri = '';
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }, () => {
console.log("we are connected");
})
.catch((err) => console.log(err));
Newer versions of Mongoose and MongoDB require your application port higher than 5000.
If your app running on port 3000 for example, you'll have this error message.
I just changed my PORT from 3000 to 5556 and I'm fine 🎉

NodeJS/PostgreSQL. I can authenticate using arbitrary/any password

I have the following problem. I created a PostgreSQL user app with this statement:
CREATE USER app WITH ENCRYPTED PASSWORD 'qwerty';
Then I gave it some privileges in my database bazy.
Then I access a database by authenticating as this user and using pg module like this:
const { Pool } = require('pg');
(async function() {
let pool = new Pool({
user: "app",
host: 'localhost',
database: "bazy",
password: "BS",
port: 5432
});
let client = await pool.connect();
let { rows } = await client.query("SELECT 'I love you';");
console.log(rows);
})();
The problem is that this works and gives this output:
[ { '?column?': 'I love you' } ]
But this should not work, for the password of the user is qwerty, not BS. And the thing is that any password works here.
What have I done wrong?
[EDIT]
The answer of #mike.k is 100% helpful.
The not commented (almost) part of pg_hba.conf file, which we can find following these instructions, looks like this:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
We can see here that we do not require password in any type of connections listed here, for method trust means that in order to connect to those users we are not required any password (it is ignored obviously), in order to change that we can use method password or md5 or scram-sha-256 instead, as is said in pg_hba.conf file:
# Note that "password" sends passwords in clear text; "md5" or
# "scram-sha-256" are preferred since they send encrypted passwords.
To be honest, I don't know where exactly I should change it, though. ¯\_(ツ)_/¯. So I changed all of them (of the methods to password) and it worked :) It requires passwords now.
Check your pg_hba.conf content, it might be configured to not require a password for localhost connections.
Try using the local IP address and you might see that it behaves differently. Or try connecting from another system on the same network.

Sequelize unable to connect to SQL Server through network

My setup is the following:
I have a Virtual Machine running all of my Database processes, let's call it DB-VM.
I'm currently developing at my own workstation (completely detached from DB-VM, except that we are under the same network.
I've created a valid connection string, validated by another database connection service throughout IIS and through a Data Link Properties file (.udl) and the connection.
This connection is described by the connection string as:
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=DB-VM\MY_DATABASE.
I tried to insert it into my Sequelize configuration as following:
const sequelize = new Sequelize({
dialect: 'mssql',
dialectModulePath: 'sequelize-msnodesqlv8',
dialectOptions: {
connectionString: 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=DB-VM\MY_DATABASE',
trustedConnection: true,
}
});
And then proceeded to try and authenticate through:
sequelize.authenticate().then(() => {
console.log('Connection stablished successfully!');
}).catch(err => {
console.log(err);
});
And this the error is as follows:
Notice: The database uses dynamic ports, therefore I can't specify the port through the port property.
Notice 2: The Named Pipes are disabled on my database settings, and I'm not sure if I will be able to enabled it.
Notice 3: The database is already setup to allow remote connections (it is currently used through a Webpage and works fine!
According to this line the sequelize-msnodesqlv8 library expects "Driver" to be a part of the connection string, otherwise it tries to guess. Besides that, all the examples of connection strings here1 and here2 are using either Server=myServerName\theInstanceName or just Server=myServerName. Instead of the Data Source=....
So step 1 is to fix you connection string. You could try one of the examples like:
dialectOptions: {
connectionString: 'Driver={SQL Server Native Client 10.0};Server=DB-VM;Database=MY_DATABASE;Trusted_Connection=yes;'
},
After that if you get a new error, please update the question.

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