Node-postgres подключается к локальному хосту, а не к AWS Postgres Instance. - PullRequest
0 голосов
/ 12 октября 2018

Я новичок в NodeJS и PostgreSQL, я хотел бы обратиться к вам за помощью в связи с моей проблемой подключения к AWS Postgres Instance.Мой Nodejs продолжает подключаться к моему локальному хосту, а не к AWS Postgres Instance.Ниже приведены мои 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 голосов
/ 15 октября 2018

Я наконец-то понял это, я забыл включить имя базы данных в мои настройки базы данных.

До

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

Спасибомного за попытку решить мою проблему.

...