When I use this code, I can connect successfully to my AWS RDS postgre
import pg from "pg";
import dotenv from "dotenv";
dotenv.config();
(async () => {
try {
const client = new pg.Client({
host: process.env.PG_HOST,
port: process.env.PG_PORT,
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
ssl: true,
});
await client.connect();
const res = await client.query("SELECT $1::text as connected", [
"Connection to postgres successful!",
]);
console.log(res.rows[0].connected);
await client.end();
} catch (error) {
console.error("Error occurred:", error);
}
})();
But now I want to use sequelize and I try this code
import "dotenv/config.js";
import Sequelize from "sequelize";
const dbConfig = {
HOST: process.env.PG_HOST,
USER: process.env.PG_USER,
PASSWORD: process.env.PG_PASSWORD + "",
DB: process.env.PG_DATABASE,
PORT: process.env.PG_PORT,
dialect: "postgres",
// ssl: true,
ssl: {
rejectUnauthorized: false,
},
};
console.log(dbConfig.USER);
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: dbConfig.dialect,
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
export default db;
I got errors:
Cannot connect to the database! ConnectionError [SequelizeConnectionError]: no pg_hba.conf entry for host "118.70.42.38", user "postgres", database "My_first", no encryption
and
original: error:
no pg_hba.conf entry for host "118.70.42.38", user "postgres", database "My_first", no encryption
.
I'm not sure why this happens. Maybe there is a problem with Sequelize? Any support would be appreciated.
Passing in a dialectOptions object has worked for me in the past which would look like:
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: dbConfig.dialect,
dialectOptions: { //< Add this
ssl: {
require: true,
rejectUnauthorized: false
}
}
});
This could be moved into your dbConfig object but I'll leave that up to you.
See the Dialect-Specific Things for PostgreSQL for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With