Node-postgres正在连接到localhost而不是AWS Postgres实例

时间:2018-10-12 11:41:39

标签: node.js postgresql amazon-web-services amazon-rds node-postgres

我是NodeJS和PostgreSQL的新手,我想就您连接到AWS Postgres实例的问题寻求帮助。我的Nodejs继续连接到我的本地主机,而不是AWS Postgres实例。以下是我的server.js和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"
   }
 }

1 个答案:

答案 0 :(得分:0)

我终于弄明白了,我忘了包括数据库名称就是我的数据库设置。

之前

const client = new Client({
    host: 'db-postgresql.abcdefghi.ap-southeast-1.rds.amazonaws.com',
    port: 5432,
    user: 'db_instance',
    password: 'my_password'
});

之后

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'
});

非常感谢您尝试解决我的问题。