'Error: database "root" does not exist' in Node.js - node.js

I am running NodeJS microservice that talks to Postgres database. But when I am trying to start the service I am getting below error. I do not have any idea why this error is popping up.
Error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection(rejection id:1): error: database “root” does not exist
My DB connection details:
const pg = require(“pg”);
const client = new pg.Client({
host: “txslmxxxda6z”,
user: “mom”,
password: “mom”,
db: “mom”,
port: 5025
});

I am able to resolve this myself. The issue is in connection config. It should be database but not db so this was causing the issue. PFB answer
const client = new pg.Client({
host: “txslmoxxx6z”,
user: “mom”,
password: “mom”,
//change db to database
database: “mom”,
port: 5025
});

Related

Callback is not function when trying to connect serverless Nodejs to Postgres local DB

I'm trying to connect my serverless lambda function with postgres local databasa bat I receive this error :
Uncaught Exception {"errorType":"TypeError","errorMessage":"callback is not a function","stack":["TypeError: callback is not a function"," at Connection.connectingErrorHandler
This is my code:
const client = new pg.Client({
user: 'postgres',
host: 'localhost',
database:'Local_DB',
port: 5432,
password: 'root'
});
var conn = "postgres://"+client.user+":"+client.password+"#"+client.host+":"+client.port+"/"+client.database;
await client.connect(conn);

Cannot connect to GCP Postgres database with Sequelize

I have created a database in GCP but when I try to connect it with my Node.js server on localhost I'm getting the following error:
original: Error: connect ETIMEDOUT 35.202.153.108:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '35.202.153.108',
port: 5432
}
Here is my code in db.js:
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD,
{
host: process.env.DATABASE_HOST,//35.202.153.108
dialect: 'postgres',
}
);
module.exports = sequelize;
db.authenticate()
.then((t) => console.log('Connection has been established successfully.'))
.catch((error) => console.error('Unable to connect to the database:', error));
Can someone help me with connecting to GCP database?
Try this one:
Create a TCP connection by using Node.js
const createTcpPool = config => {
// Extract host and port from socket address
const dbSocketAddr = process.env.DB_HOST.split(':'); // e.g. '127.0.0.1:5432'
// Establish a connection to the database
return Knex({
client: 'pg',
connection: {
user: process.env.DB_USER, // e.g. 'my-user'
password: process.env.DB_PASS, // e.g. 'my-user-password'
database: process.env.DB_NAME, // e.g. 'my-database'
host: dbSocketAddr[0], // e.g. '127.0.0.1'
port: dbSocketAddr[1], // e.g. '5432'
},
// ... Specify additional properties here.
...config,
});
};
The link as well content several other examples for TCP, Socket, SQL and even how to retry connections to Cloud SQL for Postgres in Google Cloud using node.js, php and some others.
You need to use Cloud SQL Proxy to connect to your Cloud SQL from localhost. Then your app connects with the Cloud SQL Proxy with host name=localhost.
Basically, after setting up Cloud SQl Proxy, your app acts like the database is hosted locally but it is actually talking with the Proxy and Proxy is talking with your Cloud SQL instance.

Cannot connect to mysql remote database

I try to connect to a remote database using node.js and mysql:
const mysql = require('mysql2/promise');
(async () => {
try {
db = await mysql.createConnection({
host: 'jdbc:mysql://REMOTE_IP',
port: '3306',
user: '***',
password: '***',
database: '***',
});
console.log('DB connection established.');
} catch(e) {
console.log(`DB connection error: ${e}.`);
}
})();
But I get in console:
DB connection error: Error: getaddrinfo ENOTFOUND jdbc:mysql://REMOTE_IP.
I've tried to connect with the same details via workbench and I could connect.
Why doesn't this node.js code work?
According to the documentation, you should only set the host name or IP address for host:
const db = await mysql.createConnection({
host: 'REMOTE_IP',
port: '3306',
user: '***',
password: '***',
database: '***',
});
(Note: I also added a const in front of the db variable.)

How to connect to postgres in aws server with pem file?

I want to connect to postgres in Aws server. I have installed "pg" to node.
I have tried to use the connection String, but there is no way to add private key there. So I also try with
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
privateKey : require('fs').readFileSync('./ssh_keys/my_key')
})
In both cases I get connection refused.

Sequelize for Node.js: cannot authenticate for postgresql

I am having issues with setting up the sequelize module for node.js.
First I set up a username and password in postgresql:
postgres=# CREATE USER testuser WITH PASSWORD 'test';
Here is my initialize code:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('testdb', 'testuser', 'test', {
host: 'localhost',
port: 5342,
dialect: 'postgres'
});
sequelize.query('select * from mytbl').success(function(tbl){
console.log('success');
});
However, when i run this short bit of code I get the following error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: Failed to authenticate for PostgresSQL. Please double check your settings.
I am not sure what I am doing wrong? If anyone can help it would be greatly appreciated. Thanks!
This example worked when I changed the port to 5432.

Resources