How to solve the database connection error [SequelizeConnectionError]? [duplicate] - javascript

I am trying to connect to a PostgreSQL Database that I've set up in Heroku.
const { Sequelize, DataTypes, Model } = require("sequelize");
// DB Configuration
const sequelize = new Sequelize({
database: "[won't show db]",
username: "[won't show username]",
password: "[won't show password]",
host: "ec2-54-221-195-148.compute-1.amazonaws.com",
port: 5432,
dialect: "postgres",
dialectOptions: {
ssl: true,
},
});
And this is what I am getting as the output:
SequelizeConnectionError: self signed certificate

This is due to an (accidental) breaking change in node-postgres version 8 (see this GitHub issue).
The solution is to pass rejectUnauthorized: false to the sequelize connection parameters inside of dialectOptions>ssl, as described here by GitHub user jsanta, bypassing the SSL certificate check (which is okay when connecting to a trusted server over a secure connection such as on your local host or between your own servers in the same network):
const sequelize = new Sequelize({
database: "xxxxx",
username: "xxxxx",
password: "xxxxx",
host: "xxxxx",
port: 5432,
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false // <<<<<<< YOU NEED THIS
}
},
});

in my case none of the above works, I use the connection string method to apply pg configurations, so I set the query param sslmode=no-verify and I got it works
example
postgres://myuser:mypassword#myhost:5432/mydatabasename?sslmode=no-verify

It works for me (on sequelize config.json file):
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}

This works for me, in the config.json file
"development": {
"username": "dummy",
"password": "dummy",
"database": "dummy",
"host": "dummy",
"dialect": "postgres",
"dialectOptions":{
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}
}

add the following in your code...
dbRDS=false

Related

Timezone Issue In Sequelize Nodejs

I want Sequelize to use my local time for reading and writing to database.
in Sequelize config file I added timezone: "+04:30", but it is just for writing in database.
for writing in database when i add
dialectOptions: {
useUTC: false, // -->Add this line. for reading from database
},
i get this error :
Ignoring invalid configuration option passed to Connection: useUTC. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection
full config file :
const Sequelize = require("sequelize");
const sequelize = new Sequelize("db", "root", "", {
dialect: "mysql",
port: process.env.SQL_PORT,
host: "localhost",
charset: "utf8",
collate: "utf8_persian_ci",
logging: false,
dialectOptions: {
useUTC: false, // -->Add this line. for reading from database
},
timezone: "+04:30",
});
Finally I fixed it by getter in my model:
createdAt: {
type: Sequelize.DATE,
defaultValue: moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),
get: function () {
var isoDateString = new Date(this.getDataValue("createdAt"));
return new Date(
isoDateString.getTime() -
isoDateString.getTimezoneOffset() * 60 * 1000
);
},
},
and change my config file to :
const Sequelize = require("sequelize");
const sequelize = new Sequelize("db", "root", "", {
dialect: "mysql",
port: process.env.SQL_PORT,
host: "localhost",
charset: "utf8",
collate: "utf8_persian_ci",
logging: false,
timezone: "+04:30",
});

exporting consts from CommonJS app amd require them in another file

I want to export const config from config.js file in CommonJs app.
const config = {
development: {
client: 'pg',
connection: {
database: 'myDatabase',
user: 'myUser',
host: 'localhost',
password: 'password',
port: PORT,
ssl: {
rejectUnauthorized: false
}
},
server: {
host: '127.0.0.1',
port: 'PORT2'
}
}
module.exports = config;
and in index.js I require that like
var env = process.env.NODE_ENV || 'development';
const config = require('./config')[env];
const knexDB = knex({
client: config.client,
connection: {
database: config.database,
user: config.user,
host: config.host,
password: config.password,
port: config.port,
ssl: {
rejectUnauthorized: false
}
}
});
But in the config file. IntelliSense recommends changing module.exports to ES export which I don't want to do and keep the app CommonJS. also, config object in index.js I have this error :
Property 'host' does not exist on type '{ development: { client: string; connection: { database: string; user: string; host: string; password: string; port: number; ssl: { rejectUnauthorized: boolean; }; }; server: { host: string; port: string; }; }; production: { ...; }; }'.ts(2339)
How can I export config from config.js?
You're getting the wrong property of config. It must be config.development.host . give up on the vscode requesting a CommonJS module.leave it alone. You also have 2 options more to configure your constant data.
yarn add dotenv
npm install config
Check config file you Db details is inside config.development.connection but you are reading it from config.
const knexDB = knex({
client: config.development.client,
connection: {
database: config.development.connection.database,
user: config.development.connection.user,
host: config.development.connection.host,
password: config.development.connection.password,
port: config.development.connection.port,
ssl: {
rejectUnauthorized: false
}
}
But instead of this use config or env
well, I found a way to work around it. I removed connection from development and it worked! just mention I used config.connection.client and config.development.connection.client or other variables in the connection but still not working in the index.js.
so it looks like
const config = {
development: {
client: 'pg',
database: 'myDatabase',
user: 'myUser',
host: 'localhost',
password: 'password',
port: PORT,
ssl: {
rejectUnauthorized: false
}
server: {
host: '127.0.0.1',
port: 'PORT2'
}
}
module.exports = config;
and in index.js I access that like
const configure = require('./config.js')[env];
const knexDB = knex({
client: configure.client,
connection: {
database: configure.database,
user: configure.user,
host: configure.host,
password: configure.password,
port: configure.port,
ssl: {
rejectUnauthorized: false
}
}
});
thanks for everyone for thier contribution.

Heroku with Strapi, Application is not using production database

I've deployed my app to Heroku. It gives an Application Error message upon visit.
The logs gave me this:
[2021-02-15T01:04:05.882Z] debug ⛔️ Server wasn't able to start properly.
[2021-02-15T01:04:05.883Z] error Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Which according to my guess, is that its trying to use local database. I think the app is not using the database.js located in config/env/production. The application runs fine with heroku local.
Below is the database.js I set for production env:
const parse = require("pg-connection-string").parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
defaultConnection: "default",
connections: {
default: {
connector: "bookshelf",
settings: {
client: "postgres",
host: config.host,
port: config.port,
database: config.database,
username: config.user,
password: config.password,
ssl: {
rejectUnauthorized: false,
},
},
options: {
ssl: true,
},
},
},
});
Creating and printing the config var on heroku console results in expected values.
For some reason the deployment method in the strapi docs to heroku does not seem to work when you initially have set up your local database as Postgres.
I had the same problem as you and I fixed it using the NODE_ENV env variable.
Instead of creating a new production database config file in ./config/production/database.js you can simply extend the config file in ./config/database.js with the prod config and decide based on what NODE_ENV is set which one to return.
As example:
module.exports = ({ env }) => {
const parse = require("pg-connection-string").parse;
const config = parse(env("DATABASE_URL", "127.0.0.1"));
const devConfig = {
client: "postgres",
connection: {
host: env("DATABASE_HOST", "127.0.0.1"),
port: env.int("DATABASE_PORT", 5432),
database: env("DATABASE_NAME", "db_name"),
user: env("DATABASE_USERNAME", "root"),
password: env("LOCAL_DB_PASSWORD"),
ssl: env.bool("DATABASE_SSL", false),
},
};
const prodConfig = {
client: "postgres",
connection: {
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: {
rejectUnauthorized: false,
},
},
debug: false,
};
return
env("NODE_ENV", "development") === "production" ? prodConfig : devConfig
};

SequelizeConnectionError: self signed certificate

I am trying to connect to a PostgreSQL Database that I've set up in Heroku.
const { Sequelize, DataTypes, Model } = require("sequelize");
// DB Configuration
const sequelize = new Sequelize({
database: "[won't show db]",
username: "[won't show username]",
password: "[won't show password]",
host: "ec2-54-221-195-148.compute-1.amazonaws.com",
port: 5432,
dialect: "postgres",
dialectOptions: {
ssl: true,
},
});
And this is what I am getting as the output:
SequelizeConnectionError: self signed certificate
This is due to an (accidental) breaking change in node-postgres version 8 (see this GitHub issue).
The solution is to pass rejectUnauthorized: false to the sequelize connection parameters inside of dialectOptions>ssl, as described here by GitHub user jsanta, bypassing the SSL certificate check (which is okay when connecting to a trusted server over a secure connection such as on your local host or between your own servers in the same network):
const sequelize = new Sequelize({
database: "xxxxx",
username: "xxxxx",
password: "xxxxx",
host: "xxxxx",
port: 5432,
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false // <<<<<<< YOU NEED THIS
}
},
});
in my case none of the above works, I use the connection string method to apply pg configurations, so I set the query param sslmode=no-verify and I got it works
example
postgres://myuser:mypassword#myhost:5432/mydatabasename?sslmode=no-verify
It works for me (on sequelize config.json file):
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}
This works for me, in the config.json file
"development": {
"username": "dummy",
"password": "dummy",
"database": "dummy",
"host": "dummy",
"dialect": "postgres",
"dialectOptions":{
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}
}
add the following in your code...
dbRDS=false

Using express session with mongoStore and database authentication

I have an object containing config values like so:
var db = {
name: "Auth",
port: "27017",
host: "localhost",
user: "testUser",
pass: "testPassword",
secret: "62y4q8C03l3t"
}
Now I'm using express-session with connect-mongostore to store session data in my MongoDB.
app.use(session({
resave: true,
secret: db.secret,
saveUninitialized: true,
store: new mongoStore({
"db" : db.name,
"host": db.host,
"port": db.port,
"username": db.user,
"password": db.pass
})
}));
Previously I had no authentication on my Mongo databases and this worked fine. However, I have just got authentication working on my databases, and now get the following error message on the above:
sessions <MongoError: not authorized for query on Auth.system.indexes>
What roles do your user have on the Auth database? To access the indexes, the readWrite role is not enough, you should have the dbAdmin role.

Categories

Resources