【问题标题】:Node-postgres is connecting to localhost instead of AWS Postgres InstanceNode-postgres 连接到 localhost 而不是 AWS Postgres 实例
【发布时间】:2018-10-15 10:45:54
【问题描述】:

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

包.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"
   }
 }

【问题讨论】:

  • 您也应该在客户端定义中指定“数据库”属性。
  • 嗨 Mahesh,您是什么意思客户端定义中的“数据库”属性?
  • const client = new Client({ user: 'dbuser', host: 'database.server.com', database: 'mydb', password: 'secretpassword', port: 3211, })
  • 已经包含数据库设置
  • 澄清一下,这是部署在某个地方还是在本地运行?您可以将错误日志添加到您的问题中吗?

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


【解决方案1】:

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

之前

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

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

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2016-06-06
    • 2016-04-13
    • 1970-01-01
    • 2023-02-07
    相关资源
    最近更新 更多