I am new to NodeJS and PostgreSQL, I would like to seek your help regarding to my problem connecting to AWS Postgres Instance. My Nodejs keeps on connecting to my localhost instead of AWS Postgres Instance. Below are my server.js and package.json.
server.js
const express = require('express');
const { Client } = require('pg');
const app = express();
const client = new Client({
host: 'db-postgresql.abcdefghi.ap-southeast-1.rds.amazonaws.com',
port: 5432,
user: 'db_instance',
password: 'my_password'
});
client.connect((err) => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
}
})
app.listen(5000, () => {
console.log('listening on port 5000');
});
Package.json
{
"name": "NodeJS PostgreSQL",
"version": "2.0.0",
"description": "DB Connect",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"author": "Mac Pertubal",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"pg": "^7.5.0"
},
"devDependencies": {
"nodemon": "^1.18.4"
}
}
I finally figure it out, I forgot to include the database name is my database settings.
Before
const client = new Client({
host: 'db-postgresql.abcdefghi.ap-southeast-1.rds.amazonaws.com',
port: 5432,
user: 'db_instance',
password: 'my_password'
});
After
const client = new Client({
host: 'db-postgresql.abcdefghi.ap-southeast-1.rds.amazonaws.com',
port: 5432,
user: 'db_instance',
password: 'my_password',
database: 'database_name'
});
Thank you so much for trying to solve my problem.