Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: no pg_hba.conf entry for host "155.201.35.107", user "otacrrqnhvxubd", database "delgu0put0dajp", SSL off #1

Open
mrdulin opened this issue Nov 14, 2018 · 1 comment

Comments

@mrdulin
Copy link
Owner

mrdulin commented Nov 14, 2018

本项目使用的PostgreSQL RDBMS部署在Heroku上,软件服务链接: https://www.heroku.com/postgres

使用node-postgres本地开发时,链接远程数据库服务代码如下:

import { Client } from 'pg';

const client = new Client({
  host: 'ec2-54-204-40-121.compute-1.amazonaws.com',
  port: 5432,
  user: 'otacrrqnhvxubd',
  password: 'fd33f58e8a039bc5b4abc0deea60f30b3fc531e453fad8aac592a775cc27c7be',
  database: 'delgu0put0dajp'
});

async function main() {
  try {
    await client.connect();
  } catch (error) {
    console.error(error);
  }

  try {
    const res = await client.query('SELECT $1::text as message', ['Hello world!']);
    console.log(res.rows[0].message);
  } catch (error) {
    console.error(error);
  }

  try {
    await client.end();
  } catch (error) {
    console.error(error);
  }
}

main();

报错:

error: no pg_hba.conf entry for host "155.201.35.107", user "otacrrqnhvxubd", database "delgu0put0dajp", SSL off
    at Connection.parseE (/Users/ldu020/workspace/nodejs-pg-knex-samples/node_modules/pg/lib/connection.js:554:11)
    at Connection.parseMessage (/Users/ldu020/workspace/nodejs-pg-knex-samples/node_modules/pg/lib/connection.js:379:19)
    at Socket.<anonymous> (/Users/ldu020/workspace/nodejs-pg-knex-samples/node_modules/pg/lib/connection.js:119:22)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

查看文档可知,外部网络要链接到heroku上部署的PostgreSQL数据库服务,需要设置sslmode=require,修改后的代码如下:

const client = new Client({
  host: 'ec2-54-204-40-121.compute-1.amazonaws.com',
  port: 5432,
  user: 'otacrrqnhvxubd',
  password: 'fd33f58e8a039bc5b4abc0deea60f30b3fc531e453fad8aac592a775cc27c7be',
  database: 'delgu0put0dajp',
  ssl: true // required
});

再次执行代码:

  getting-started [master]   npx ts-node index.ts
Hello world!
@awalias
Copy link

awalias commented Mar 20, 2020

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants