【问题标题】:SSL connection is required. Please specify SSL options and retry需要 SSL 连接。请指定 SSL 选项并重试
【发布时间】:2020-12-25 17:37:36
【问题描述】:

我使用 azure postgres,所以当我禁用 SSL 时它工作正常,但我希望启用 SSL 连接,当我从 azure 启用时 然后当我启动我的应用程序错误:SSL connection is required. Please specify SSL options and retry 这个错误发生多次

在 azure 文档中的位置 Azure Docs for SSL . 这是我的连接字符串

connection: {
        database: process.env.POSTGRES_DATABASE || 'postgres',
        port: process.env.POSTGRES_PORT || 5432,
        user: process.env.POSTGRES_USER || 'my@-username',
        host: process.env.POSTGRES_HOST || 'postgres.db.postgres.database.azure.com',
        password: process.env.POSTGRES_PASSWORD || 'postpass',
        sslmode: 'require',
        }

根据 azure docs 我指定 sslmode:require 但它不起作用 有没有人遇到同样的错误并得到了解决方案,然后告诉我,我从过去 5 天开始就陷入了这个错误。

【问题讨论】:

  • 试试ssl: true而不是sslmode: 'require'
  • 尝试连接不同的客户端(如psql),看看是否出现同样的错误。
  • 您需要提供您的 ssl 证书。
  • @kirtan 您还有其他顾虑吗?

标签: node.js postgresql azure ssl tls1.2


【解决方案1】:

关于问题,请参考以下步骤

  1. 下载SSL certificate。更多详情请参考here

  2. 代码

const pg = require("pg");
const fs = require("fs");
const config = {
  host: "<>.postgres.database.azure.com",
  user: "<>",
  password: "<>",
  database: "postgres",
  port: 5432,
  ssl: {
    cert: fs.readFileSync("<the cert path>"),
    rejectUnauthorized: true,
  },
};

const client = new pg.Client(config);

client.connect((err) => {
  if (err) throw err;
  else {
    console.log("success");
  }
});
const query = "SELECT * FROM inventory;";

client
  .query(query)
  .then((res) => {
    const rows = res.rows;
    console.log(`Read: ${JSON.stringify(rows)}`);
    process.exit();
  })
  .catch((err) => {
    console.log(err);
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多