I am new to AWS and NodeJS/Express server. The server should connect to AWS RDS Postgress Database.
I am developing using Visual Studio Code on MacOS
I get this error when I try to run my server:
Using ts-node version 8.4.1, typescript version 3.6.3
(node:2377) UnhandledPromiseRejectionWarning: SequelizeConnectionError: password authentication failed for user “userNB”
at connection.connect.err (…node_modules/sequelize/lib/dialects/postgres/connection-manager.js:154:24)
at Connection.connectingErrorHandler (…..node_modules/pg/lib/client.js:191:14)
at Connection.emit (events.js:198:13)
at Connection.EventEmitter.emit (domain.js:448:20)
I am saving my username, pass,...etc as environment variables in /.bash_profile
my Sequelize code:
import {Sequelize} from 'sequelize-typescript';
import { config } from './config/config';
onst c = config.dev;
export const sequelize = new Sequelize({
"username": c.username,
"password": c.password,
"database": c.database,
"host": c.host,
dialect: 'postgres',
storage: ':memory:',
});
Is this kind of error related to my NodeJS code or AWS settings?
How can I fix it ?
I'm using sequelize like this and it's working for me:
DATABASE_URI=postgresql://username:password#amazonhost:5432/database_name
export const sequelize = new Sequelize(DATABASE_URI);
Also make sure you've the correct password and the IP from which you're trying to connect is whitelisted for database. You can test this using postgres client cli.
Let me know if it works for you.
Thanks!
you are missing sequelize params to create a new instance. create a new sequelize instance like this:
const sequelize = new Sequelize('database', 'username', 'password', {
host: your_database_host,
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
for more information, see sequelize docss
https://sequelize.org/master/manual/getting-started.html
Related
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.
I rented an EC2 instance of Ubuntu 16.xx on AWS and installed PostgreSQL on it. I created a database and table inside the PostgreSQL on EC2. Right now I am trying to connect to and get data from the database via a local Node.js project using knex.
I already enabled the inbound rule for port 5432 to IP from anywhere.
However, it returns error message as below:
Error: connect ECONNREFUSED 13.229.xxx.xxx:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1142:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '13.229.xxx.xxx',
port: 5432
}
How am I gonna fix it? Do I need to install and implement a reversed proxy? If so, how do I set it up? I know there is RDS on AWS, but I need to use EC2 to implement it.
Here are some of my codes:
This is the knex setting, I have pg installed. The connection to a local database is successful. But when I switch the host to whether a public IP/ private IP/ ec2-13-229-xxx-xxx.ap-southeast-1.compute.amazonaws.com. They all return the above error message.
development: {
client: 'postgresql',
connection: {
host: '13.229.xxx.xxx',
database: 'project2',
user: 'postgres',
password: 'postgres',
port: 5432,
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
This is the Node.js code that I used to connect to the server. A very simple one, just to test the connection.
const express = require('express');
const hbs = require('hbs');
const app = express();
const knexConfig = require('./knexfile')['development'];
const knex = require('knex')(knexConfig);
let query = knex.select('*').from('users');
query
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
This is my firewall setting which is turned off
Also, I paused my Kaspersky.
This is my pg_hba.conf file
And I am not sure where to add the permission of my personal IP.
This issue was related to the pg_hba.conf being restricted to localhost only.
Additionally the postgres.conf needed to have listen_addresses = '*'.
By whitelisting outside access, it was possible to access the database.
Additional support from this article.
I am trying to connect ot mssql with Sequelize using
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mssql'
});
or
const sequelize = new Sequelize('msssql://user:pass#example.com:5432');
and getting Dialect needs to be explicitly supplied as of v4.0.0
I do not use CLI just getting started code.
regars
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
});
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.