Node-postgres 正在连接到本地主机而不是 AWS Postgres 实例

Node-postgres is connecting to localhost instead of AWS Postgres Instance

我是 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"
   }
 }

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

之前

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

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