skip to Main Content

I have created an instance of RDS Postgres on database. I am trying to use it in my nodejs application as remote database. The things require to make connection to db include endpoint, username, password, port and I’ve got all of them. following is how i stored URL in .env file:

POSTGRES_URL=postgres://<username>:<password>@<end-point>:5432/<db>

In my node application, I have configured the database as following:

database.ts

import { Sequelize } from 'sequelize';

const dbUrl:string = process.env.POSTGRES_URL || '';

const sequelize: Sequelize = new Sequelize(dbUrl, {
    dialect: 'postgres',
});

export default sequelize;

index.ts

import sequelize from "./config/database";
const connectDB = async (): Promise<void> => {
    try {
        await sequelize.authenticate();
        console.log('DB connected successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
};
//connect db then run server
connectDB().then(() => {
    app.listen(process.env.PORT, (): void => {
        console.log(`Server is running on port ${process.env.PORT}`);
    });
});

when I run cmd npm run dev i get error like this:

original: error: no pg_hba.conf entry for host <my-ip>, user <username>, database <db>, no encryption.  
at Client._connectionCallback (/<path>/node_modules/sequelize/src/dialects/postgres/connection-manager.js:196:24)

All variables inside <> symbols above are same as in URL.

I tried to run db through Ubuntu terminal using following command:

psql --host=<end-point> --port=5432 --username=<username> --password

It asks for password and as I enter password, it starts without any error.
Problem only arises in nodejs application.

2

Answers


  1. That error means that the client has reached the server, but authentication failed for some reason (pg_hba.conf is the configuration file that defines how clients are authenticated). PostgreSQL gives you this generic error message, so that attackers cannot glean too much information.

    You should look into the PostgreSQL server’s log; it should contain a corresponding error message that may provide more detailed information. If you find the same error message there, it means what it says: there is no entry in pg_hba.conf that matches the incoming connection request, so the connection is rejected.

    Login or Signup to reply.
  2. You need to turn on SSL (encryption) when connecting to Amazon RDS Postgres.

    That’s why it says: no pg_hba.conf entry for host <my-ip>, user <username>, database <db>, no encryption.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search