Search code examples
node.jspostgresqlamazon-rdspg

NodeJs postgresql connection failure


  • I try to connect to rds postgresql version 12 from nodeJs as mentioned below. It works completely fine without any issue.

    const config = {
    
        user: process.env.PG_DB_USERNAME,
        password: process.env.PG_DB_PASSWORD,
        database: process.env.PG_DB_NAME,
        host: process.env.PG_DB_HOST,
        port: process.env.PG_DB_PORT,
     }
       const pool = new Pool(config)
    

When we use postgresql version 15.2 ton connect from node Js we ended up facing the below error.

Error

{"length":181,"name":"error","severity":"FATAL","code":"28000","file":"auth.c","line":"543","routine":"ClientAuthentication","level":"error","timestamp":"2023-06-19T14:13:33.294Z"}

[ERROR] error: no pg_hba.conf entry for host "XXXXX", user "test", database "testdb", no encryption

at Parser.parseErrorMessage (/usr/src/app/node_modules/pg-protocol/dist/parser.js:287:98)
at Parser.handlePacket (/usr/src/app/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/usr/src/app/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/usr/src/app/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:526:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

I tried passing the below params as suggested in various stack overflow links, but ended with same error.

We tried deploying the code as suggested in the below link , but it's not resolved.

Reference: Node.js, PostgreSQL error: no pg_hba.conf entry for host

"dialectOptions": { "ssl": true }

ssl: true

Could you please help in fixing this error?


Solution

  • The problem is , before version 15, postgresql has the default paramater value ( db parameter group ) for force_ssl as 0.

    Starting from version 15 they made this default value to 1.

    Setting it to 0 resolved this Issue.

    From aws doc,

    You can require that connections to your PostgreSQL DB instance use SSL by using the rds.force_ssl parameter. The default rds.force_ssl parameter is set to 1 (on) for RDS for PostgreSQL version 15. All other RDS for PostgreSQL major version 14 and older have the default value for rds.force_ssl parameter set to 0 (off). You can set the rds.force_ssl parameter to 1 (on) to require SSL for connections to your DB instance.

    Reference: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL.Concepts.General.SSL.html