Render Hosted Postgres database throws ECONNRESET on any connection attempt Node Express App - node.js

I am unable to query my Postgres DB in Render. I researched this issue online and found that it was a common problem but none of the solutions I found worked.
Here is my server-side code (I am using NodeJS, Express, Typescript and Postgres)
import postgres, { RowList, Row } from 'postgres'
import appconfig from '../app.config'
type Query = (sql: string) => Promise<RowList<Row[]>>
const query: Query = async (sql) => {
try {
const q = postgres({
host: appconfig.database.host,
port: appconfig.database.port,
database: appconfig.database.schema,
username: appconfig.database.username,
password: appconfig.database.password,
})
const res = await q`${sql}`
return res
} catch (err) {
throw err
}
}
export default query
I receive the following error every time and have not had a successful attempt. It's worth noting I have no issues connecting from PGAdmin on the same PC with the same credentials
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
at cachedError (C:\Users\xxxxx\repos\one-watch\node_modules\postgres\cjs\src\query.js:171:23)
at new Query (C:\Users\xxxxx\repos\one-watch\node_modules\postgres\cjs\src\query.js:36:24)
at sql (C:\Users\xxxxx\repos\one-watch\node_modules\postgres\cjs\src\index.js:111:11)
at C:\Users\xxxxx\repos\one-watch\src\database\query.ts:15:24
I have never used postgres before, all of my database experience has been in mysql up to this point, although I don't expect this is a postgres problem but potentially just a nuance of Render's implementation of their Postgres service. Any help would be greatly appreciated, thanks!
The only articles I've found like this one are related but they are able to get at least some sort of successful connection at least once. In my case they are all failing.

Adding
?sslmode=no-verify
in the end of the url worked for me.

Related

Mongodb Client Connection TypeError NULL issue

Working on a simple nodejs express app using Mongodb. I am getting a typeerror cannot read from null when I try to work with the connection object that is returned. The connection object isn't null so I am not clear on what the error message is actually trying to point out.
the line of code that is generating the error is:
const silos = conn.db('toolkit').collection('silos')
I have a debug console.log right before that of:
console.log("Connection: ",conn)
I am checking for an error on the connect callback and the error is null. Prior to this issue I had an issue with the connection and username/authentication so I know the error checking works as before it was triggering on bad logins.
The error is:
TypeError: Cannot read property 'db' of null
at mongoClient.connect (/var/projects/drake/Routes/Silos.js:22:28)
at err (/var/projects/drake/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback (/var/projects/drake/node_modules/mongodb/lib/utils.js:404:25)
at executeOperation (/var/projects/drake/node_modules/mongodb/lib/utils.js:422:7)
at MongoClient.connect (/var/projects/drake/node_modules/mongodb/lib/mongo_client.js:168:10)
at getSilos (/var/projects/drake/Routes/Silos.js:18:17)
at Layer.handle [as handle_request] (/var/projects/drake/node_modules/express/lib/router/layer.js:95:5)
at next (/var/projects/drake/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/projects/drake/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/projects/drake/node_modules/express/lib/router/layer.js:95:5)
the console.log line generators this message:
Connection: MongoClient {
Here is the code that I use for the Mongo connection. This is in a file and the MongoClient object is exported that is used in other files to connect to the database.
const url = `mongodb://${process.env.MONGO_HOST}:${process.env.MONGO_PORT}`
const opts = {
useNewUrlParser: true,
authMechanism: process.env.MONGO_MECH,
authSource: process.env.MONGO_SRC,
auth: {
user: process.env.MONGO_USER,
password: process.env.MONGO_PWD
}
}
const mongoClient = require('mongodb').MongoClient;
const objectID = require('mongodb').ObjectID
const mongo = new mongoClient(url,opts)
module.exports.mongoClient = mongo
Here is the where I pull in that code and call the connect.
Importing the code:
const { mongoClient } = require('../mongo')
Using the imported code:
mongoClient.connect( (err, conn) => {
if (err) err(res,err, "Database Connection Error", 500)
console.log("Connection: ",conn)
const silos = conn.db('toolkit').collection('silos')
this last line is the one that gives the error.
Results of the console.log
Connection: MongoClient {
One common cause of this is when you try to interact with the mongodb server before a connection is actually made. I might be wrong but I suspect you might be calling your db operations in the wrong order. To avoid this, make sure you have actually successfully connected to the mongodb server before running queries on the collection. If you are using mongoose, then
mongoose.connect('/a/valid/mongo/uri')
will make
mongoose.connection
a valid object that can be interacted with.
Could you please update your question with your connection handling code?
No idea what this was about.
I appreciate the feedback that I got to this.
I spent a weekend rewriting the code from scratch and it worked this time. I did the same thing I believe. There must of been a typo that I was overlooking.
For what it's worth I want to share my experiences with this problem. I was using Mongoose to make the connection but it would crash on either a "replica set" or an "auth error". Eventually I decided to move to MongoClient, just like you did. This is because "Mongoose will not throw any errors by default if you use a model without connecting.".
Moving to Mongo the connection error also returned 'null'. I searched and searched and tried several things, including copying your code and adding the 'auth' option, etc. I eventually switched back to Mongoose (though that shouldn't matter for what solved it for me) and did this:
Create a new user with ReadWrite abilities (didn't help immediately but is definitely needed, since it gave an error when deploying to Heroku without the correct 'new' user.). I now have two users in my MongoDB account.
Add the following to my (Mongoose) Schema:
///Already had this:
let testSchema = new Schema {
foo: String,
bar: Number
}
///Added:
,{
bufferCommands: false,
autoCreate: false
});
Since the DB was still empty I added the following code after creating the model.
///Already had this:
let Test = mongoose.model('Test', testSchema);
///Added:
Test.createCollection();
I believe it then initiated the collection 'for real'. In the mongo shell I know that a collection is not visible until there is an item in there. Now that the app.js didn't crash upon starting I managed to add a document to the database using a form in my HTML and it worked. I now have a working DB connected to my app. Hope this could help anyone also landing on this page.

Depreciation error with UR parser and connection error, incomplete key value pair, mongo

I'm having a weird issue with Mongo throwing me errors that I can't seem to resolve. It's weird, because my code still work. If I hit my end points I get my data, all is great, right? But I'd still rather not have some error hanging over me that can be an issue down the line.
I did do some research on these errors on SO, specifically I found: Unhandled promise rejection: Error: URL malformed, cannot be parsed, and Incomplete key value pair for option remote mongodb, but no solutions that seemed to work.
My Mongo is version 4.0.2. I'm connecting through Mongoose, v5.3.9.
This is the error:
(node:5305) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
connection error { MongoParseError: Incomplete key value pair for option
at parseQueryString (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:387:13)
at parseConnectionString (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:447:21)
at QueryReqWrap.dns.resolveTxt [as callback] (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:127:7)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:245:10)
name: 'MongoParseError',
[Symbol(mongoErrorContextSymbol)]: {} }
Here's the connection code, most of it pulled and modified directly from the Mongoose getting started guide, as I've never used Mongoose before:
const mongoose = require('mongoose');
const user = process.env.MONGO_USER;
const pw = process.env.MONGO_PW;
const uri = `mongodb+srv://${user}:${pw}#cluster0-vji0s.mongodb.net/100days?test?retryWrites=true`;
// wrap the connection in a closure so I can export it with the URI
function Connect(uri) {
return function() {
mongoose.connect(uri, {
auth: {
user: user,
password: pw
}
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', ()=> {
console.log('\nSuccessfully connected to Mongo!\n');
});
}
}
const MongooseConnect = Connect(uri);
module.exports = MongooseConnect;
And in my server (Express JS), I connect once my server is running:
const MongooseConnect = require('./database.js');
//... other express stuff
app.listen(port, ()=> {
MongooseConnect();
console.log(`listening on port ${port}\n`)
});
The code works, I connect and hit my data base and do CRUD just fine. I haven't always gotten this error (well, not the key-pair one, I've always had the deprecation warning, and just kicked the can down the road for later), but I have no idea what triggered to change it. The most recent changes I've made was to move my schemas to their own folder, I don't see why that would cause this. I don't know what causes these error messages to crop up, or what to do to make them go away. Everything I've tried in the links I made above don't work or make things worse.
Any help would be appreciated
Also, my username and password do not use any special characters, only numbers and letters.
To fix the newUrlParser error you should just have to change your mongoose connect call to:
mongoose.connect(uri, {
auth: {
user: user,
password: pw
},
useNewUrlParser: true
});
I'd see if this fixed the other issue just to be sure.
Edit: I think I know whats causing the other issue
Your connection string is:
mongodb+srv://${user}:${pw}#cluster0-vji0s.mongodb.net/100days?test?retryWrites=true
The portion in question is: /100days?test?retryWrites=true
This is not a proper query string. I am not sure if you were trying to assign a test param, but try this for now /100days?retryWrites=true
If you do need multiple params in a query string, the syntax is /100days?retryWrites=true&secondParam=value&thirdParam=thing...
The error was saying the test param has no value, since there is no =value part. It probably threw away the entire query string. So your code should have run as normal, just without the retryWrites=true part being set.
Hope that helped!
There are two different errors here.
To fix the current URL string parser is deprecated, see Brandon's answer (use userNewUrlParser).
To fix the MongoParseError: Incomplete key value pair for option, the connection string you have there is not a valid URI:
const uri = `mongodb+srv://${user}:${pw}#cluster0-vji0s.mongodb.net/100days?test?retryWrites=true`;
Note the /100days?test?retryWrites... part. The error was saying that test is not a valid key-value pair. Either remove 100days or remove test, e.g.:
mongodb+srv://${user}:${pw}#cluster0-vji0s.mongodb.net/test?retryWrites=true
or:
mongodb+srv://${user}:${pw}#cluster0-vji0s.mongodb.net/100days?retryWrites=true
See Uniform Resource Identifier for information regarding the URI format.
Just change the password that you use ,and is work

Error when attempting knex seed:run after successful knex migrate:latest for remote database

I'm running the following error when attempting to run knex seed:run against my remote postgres database (not localhost): Knex:Error Pool2 - Error: connect ECONNREFUSED 127.0.0.1:5432.
I am able to run knex migrate:latest successfully and can see that the tables are created on my postgres server, but when I try to seed I get that error. I've run the same migrations/seed file against my local configuration and it has worked without a problem, but when I attempt to seed my heroku postgres instance, it throws this error (I'm not running my local pg service when I'm seeding the new db, which is likely why it's throwing an error).
Any thoughts on why it is attempting to connect to localhost instead of the specified db? Sample of my file provided below:
var User = require("./models/User");
var Project = require("./models/Project");
exports.seed = function(knex, Promise) {
console.log(knex.client.config.connection); //This returns the correct db info.
return knex('user').del()
.then(function() {
return knex('project').del()
}).then(function() {
return new User({id: 1, firstName: "James", lastName: "Lee", phone: "123-456-2000", email: "test#test.com"}).save(null, {method: "insert"});
}).then(function() {
return new Project({id: 1, name: "Test"}).save(null, {method: "insert"});
})
};
This seems to have occurred due to how I was setting up the migrations / seeds. The configurations were actually pulling from two different places, one which had the correct SSL settings in place, the other without (seed file). Adding the correct settings in both places seemed to resolve the issue.

Trying to create a connection in Node.js using the npm light-orm and PostgreSQL (pg)

I am trying to create a postgreSQL connection using the light-orm.
I have it working in MySQL using:
var mysql = require('mysql'),
lightOrm = require('light-orm');
lightOrm.driver = mysql.createConnection({
host: "localhost",
user: "me",
password: "secret",
database: "test"
});
lightOrm.driver.connect();
However I cannot seem to get it to work with PostgreSQL.
What I have is:
var pg = require('pg'),
lightOrm = require('light-orm');
lightOrm.driver = new pg.Client('postgres://me:secret#localhost/test');
lightOrm.driver.connect();
I know that the connection is happening, because if I change the password to something incorrect, I get an error. But when trying to use the same code that works with MySQL, I either get an error, or nada.
My guess is this problem stems from my lack of knowledge of the pg module and not a light-orm issue.

Node.js and node-sqlanywhere - No Connection Available

I have been trying to use node-sqlanywhere to query my Sybase database.
However, I cannot seem to be able to connect to the database.
var sqlanywhere = require('sqlanywhere');
var sqlanywhere_conn = sqlanywhere.createConnection();
var sqlanywhere_conn_params = {
DatabaseFile: 'C:\Users\support\Dropbox\database.db',
AutoStart: 'YES',
UserId: 'user_id',
Password: 'password'
};
sqlanywhere_conn.connect(sqlanywhere_conn_params, function(){
sqlanywhere_conn.prepare('SELECT * FROM some_table WHERE some_column = ?', function (err, stmt){
if (err) throw err;
// do something with the statement
});
});
Whether I am trying to connect to the database using Server attribute or the DatabaseFile attribute, I always get error code -2005 No Connection Available.
That being said, I am able to connect to my database using Interactive SQL by either specifying the server or the database file.
So.. I must be missing something here and I can't figure it out !
Thanks !!
Try doubling up the backslash characters, like DatabaseFile: 'C:\\Users\\support\\Dropbox\\database.db'.
FYI you can also ask SQL Anywhere-specific questions on http://sqlanywhere-forum.sap.com.
Full disclosure: I work for SAP in SQL Anywhere engineering. I am responsible for both the node.js driver and the sqlanywhere-forum.sap.com site.

Resources