node pg.connect never calling its callback, how to debug? - postgresql

So I have a working express server talking to a postgres backend running on a heroku server. I'm trying to move to AWS. On AWS I think I have everything set up correctly. I have my express server running (under PM2 though that shouldn't make a difference. I can SSH into the ec2 and from there launch a psql and connect to the DB without any issues, I've created my schema no problem.
However my app is not working with the DB, Most likely its not even connecting but I'm getting nothing back. not even a timeout. I first tried with env variables to set everything up but decided to put the user/password in the code.
my express code looks like this obviously i changed the values for the post but they are the same values
I use on the psql command line
const {Client} = require('pg');
const db = new Client({
user: 'myUserName',
host: 'myHostName',
password: 'myPassword',
});
console.log("about to connect to the DB");
db.connect(err => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
}
}
);
In my logs I get the line "about to connect to the DB" but I never get anything else I do not
get the error nor the success line.
how do I see what's wrong? with no feedback I'm stuck. Is there something I can check in aws?
I doubt its a security group issue since psql works

Seems I had a bad version/installation of Node. I created a new instance on AWS and installed Node 12.x instead of "latest" which was 14.x and its working fine now.

Related

Prisma DB Can't connect to AWS RDS

I have a nextjs project that's using prismaDB for the ORM. I'm able to connect just fine to my local postgres db but I'm getting this error when running npx prisma migrate.
Error: P1001: Can't reach database server at db-name.*.us-west-2.rds.amazonaws.com:5432.
schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
//url = "postgresql://master_username:master_password#aws_host:5432/db_name"
}
The RDS db is currently public and I'm positive that I've copied over the RDS credentials correctly. There doesn't seem to be anything I should be including for the connection to work but I'm not getting any other info as to why I can't reach the db server.
Seems like you have to replace db-name.*.us-west-2.rds.amazonaws.com with the name of your actual database, unless you replaced it for the purpose of asking this question. Specifically the part where it says db-name.*.
Docs: https://www.prisma.io/docs/reference/api-reference/error-reference#common
P1001 indicates that it couldn't find the database given the connection string, NOT necessarily that the credentials you provided were wrong. Make sure you're specifying the correct database name/host and whatever else you need to make it work for AWS.
Somehow I was able to connect to RDS after deleting and creating a new DB for the third time. I confirmed connection through pgAdmin then tried it again my app deployed to vercel.

AWS Postgres DB "does not exist" when connecting with PG

I can't seem to connect to my DB instance in AWS. I'm using the pg package and following the examples from the website is not working.
A search for "aws postgres database does not exist" really isn't returning anything helpful. Going through the open/closed issues on the PG github isnt helpful either.
Running $nc <RDS endpoint> <port number> returns a success message so it's definitely there. Every value placed in the Client config is copy/pasted from my DB instance.
I'm starting to wonder if the databases have a different name than what it shows in the "Instances" section of RDS on AWS?
const client = new Client({
host : '<<RDS ENDPOINT>>',
database : '<<RDS NAME>>', // maybe this isnt the real name?
user : '<<username>>',
password : '<<password>>',
port : <<port>>
});
client.connect()
.then(data => {
console.log('connected');
})
.catch(err => {
console.log(err);
})
I ran into this issue as well. It looks like the DB Instance Name and the actual DB name are two different things and even when you add a name when you create your DB, it defaults to 'postgres'. When I put in the name of my DB it gave me the same error. However, when I just put in 'postgres' it worked fine. Try that and see if it works for you.
The initial configuration of RDS instances is quite messy, since the parameter "database name" is only the name of the instance, not the proper name of the database. If you want AWS to create a database at the moment you create the db instance, you have to select "Additional configuration" and explicitly add a parameter called "Initial database name". Check the screenshot I attach here.
Try adding postgres as dbname. It worked for me!
After connecting with postgres as db name, you can type \l to list all database on that PSQL cluster, that will return a bunch of default dbs and also the one you created (the name) so you can connect to it
I ran into the same issue after creating a DB instance on AWS RDS. I wanted to test the connection of my database using PostBird, and I used my actual DB instance name but it could not work.
But I used "postgres in field of DB_name and it worked. That means that my default username was posgres and db_name was also "posgres.
I hope it will help you too.
Try this if the above answer does not work.
Remove the:5439/lab ending so that the Host value ends with: .com

Mongoose : Failed to connect to mongodb instance, but mongo works fine

I'm facing a quite odd issue today.
I have a MongoDB database working just fine on a remote server, say 1.2.3.4, running on port 22222.
When I use the mongo cli to connect via the command line interface, everything works as expected:
mongo --host=1.2.3.4 --port=22222
But when I try to connect to the same instance using mongoose:
var options = {
server: {},
replset: {}
};
options.server.socketOptions = options.replset.socketOptions = { keepAlive: 120 };
mongoose.connect('mongodb://1.2.3.4:22222/test', options);
I get this error:
failed to connect to server [1.2.3.4:22222] on first connect
Anybody knows why?
FYI the all setup is in my company, which happens to have a corporate proxy.
I was thinking that maybe the proxy was the evil one in this case, but then why the mongo cli connection is working just fine?
Do you have a db called test? And have you tried omitting the options?
i.e. mongoose.connect('mongodb://1.2.3.4:22222/test');

setting up graphQL on OpenShift

I'm learning how to set up a server on OpenShift that uses node, express, GraphQL and Postgres and I need some help.
When trying to set up the GraphQL Server I've been following this and it works on my local machine:
import Express from 'express';
import GraphQL from 'express-graphql';
import Schema from './db/GQLschema';
const app = Express();
app.use('/graphql', GraphQL({
schema: Schema,
pretty: true,
graphiql: true
}));
...
I'm using the server.js template provided by OpenShift so the above changes to:
...
self.app = express();
self.app.configure(function() {
self.app.use('/public', express.static(__dirname+'/public'));
self.app.use('/graphql', GraphQL({
schema: Schema,
pretty: true,
graphiql: true
}));
});
...
But this doesn't work when pushed up to OpenShift. I get 'Internal Server Error' instead of the graphiql interface.
I'm new to this, but here are some guesses as to what it might have to do with:
Connecting to database
Missing dependencies
Connecting to database: I have 2 json config files. One for my local machine which is connected using a port-forward SSH tunnel to OpenShift. That works fine. The other is one for production. In it I have all the strings for the database name, user, port, host, and password, that OpenShift provided me. I'm using the config library which looks at NODE_ENV to decide which config json file to use. I set the NODE_ENV variable on OpenShift to production. When I query it, it shows all the correct strings. Is there something else I need to set?
Missing dependencies: To keep it neat, I am keeping my back-end code separate from my front-end code. So the back-end code has only the express, graphql, sequelize, config, and pg stuff installed. Do I need something else to make the graphiql page work? Also, I put all the /node_modules/ in the .gitignore file -- I assumed that OpenShift installs them post push. Was that the right thing to do?
Would appreciate any help!
OK - I solved it by doing the following:
in the openshift cli tools turn on error logs
rhc tail -a <yourappname>
This will give you some clues. In my case it was a message from express-graphql "unresolved promise". After researching I found that if you install and save es-promise and add this line to the top of the server.js file:
require ('es6-promise').polyfill();
Hope that helps someone else!

NodeJS, ExpressJS & PassportJS Sessions on Cloud9 connecting to Appfog Mongo

I'm having great difficulty getting any sort of connection to MongoDB on Appfog for session storage working. I'm currently running Node Express with PassportJS and Mongoose. I've followed numerous examples and tutorials online and tried a few ODM mappers.
I must confess, I've struggled to get any sort of connection let alone, the abstracted sessions stuff working.
I've tried the SO examples here and here without success. Everytime I attempt to connect with the following code:-
var env = process.env.NODE_ENV || 'development',
config = require('./config/config')[env],
auth = require('./config/middlewares/authorization'),
mongoose = require('mongoose');
// Bootstrap db connection
mongoose.connect(config.db);
I get a:-
Error: Error setting TTL index on collection : sessions
at module.exports._get_collection (/var/lib/stickshift/514a22705973cafc85000110/app-root/data/447240/node_modules/connect-mongo/lib/connect-mongo.js:137:23)
at Db.ensureIndex (/var/lib/stickshift/514a22705973cafc85000110/app-root/data/447240/node_modules/mongodb/lib/mongodb/db.js:1227:28)
at Db.indexInformation (/var/lib/stickshift/514a22705973cafc85000110/app-root/data/447240/node_modules/mongodb/lib/mongodb/db.js:1371:30)
Can anyone offer any possible direction or help to get this working please?
Please ask if you need further dumps of the code here to aid a solution.
Help appreciated.
I had the same problem and it happened because I was using a partially connected db to initialize express.session().
This is what I had initially:
mongoose.connect(dbPath);
...
app.configure(function() {
...
app.use(express.session({
secret : secret,
store: new MongoStore({ db: mongoose.connection.db })
}));
...
});
The 'new MongoStore()' call happens before mongoose.connect() finishes so it uses the not-yet-connected db to set the TTL for 'sessions' and fails. I switched to "new MongoStore({ url: dbPath })" and that fixes the issue but probably results in 2 different db connections.
I was getting the same error, got past it by using the code in this question to get more error info dumped out; it helped me realize that the problem really was my user Id and password in the connect string.
I couldn't get the connection to AppFog to work. Unfortunately, AppFog themselves were unhelpful also. Therefore, I spun up a MongoHQ instance and no problems connecting at all.