2

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"
   }
 }
6
  • You should specify the "database" attribute in the Client definition too. Commented Oct 12, 2018 at 11:49
  • Hi Mahesh, what do you mean "database" attribute in the Client definition? Commented Oct 12, 2018 at 11:56
  • const client = new Client({ user: 'dbuser', host: 'database.server.com', database: 'mydb', password: 'secretpassword', port: 3211, }) Commented Oct 12, 2018 at 11:57
  • database settings are included already Commented Oct 12, 2018 at 11:57
  • To clarify, is this deployed somewhere or are you running it locally? Can you add the error logs to your question? Commented Oct 12, 2018 at 21:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.