gpt4 book ai didi

node.js - Node-postgres 连接到本地主机而不是 AWS Postgres 实例

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:54 25 4
gpt4 key购买 nike

我是 NodeJS 和 PostgreSQL 的新手,我想就连接到 AWS Postgres 实例的问题寻求您的帮助。我的 Nodejs 继续连接到我的本地主机而不是 AWS Postgres 实例。下面是我的 server.js 和 package.json。

服务器.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');
});

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

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

关于node.js - Node-postgres 连接到本地主机而不是 AWS Postgres 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52778792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com