在连接 PostgreSQL 数据库时需要设置 SSL 选项为禁用(off)。示例代码如下:
const pg = require('pg'); const client = new pg.Client({ user: 'your_username', host: 'localhost', database: 'your_database', password: 'your_password', port: '5432', ssl: { rejectUnauthorized: false } });
client.connect(err => { if (err) throw err; console.log('Connected to PostgreSQL database!'); });
在示例代码中,将 ssl 选项设置为禁用(off)即可解决问题。具体做法是在 ssl 对象中添加属性 rejectUnauthorized,并将其设为 false。这样,在连接时就不会因 SSL 的设置问题报错而导致连接失败。