我是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"
}
}
答案 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'
});
非常感谢您尝试解决我的问题。