How to connect and query PostgreSQL RDS from EC2 using NodeJS?

Viewed 4139

How to use NodeJS to create connection from EC2 to remote RDS (PostgreSQL) and query it?

I have this code but currently does not work (how exactly connection string should look like including password?):

var pg = require('pg');
var conString = "postgres://username:@db.us-east-2.rds.amazonaws.com:5432/testdb";

var client = new pg.Client(conString);
client.connect();

var query = client.query("SELECT * FROM t1");
//fired after last row is emitted

query.on('row', function(row) {
    console.log(row);
});

query.on('end', function() {
    client.end();
});
1 Answers
const connUrl = postgres://{username}:{passoword}@{endpoint}:{port}/{database} 
Related