环境:
Windows 10, localhost, same machine
pg 12
node 14
openssl 1.1.1k
我已阅读并完成了从这里开始的pg文档。 postgresql.conf(位于C:\Program Files\PostgreSQL\12\data中,我的理解是它控制pg数据库服务器)。
ssl = on # per pg doc: server will listen for both normal and SSL connections on the same TCP port, and will negotiate with any connecting client on whether to use SSL
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL'
ssl_prefer_server_ciphers = on
ssl_ca_file = 'root.crt' # per pg doc, 18.9.3: To require the client to supply a trusted certificate
ssl_crl_file = ''
pg_hba.conf(位于C:\ Program Files \ PostgreSQL \ 12 \ data中,我的理解是它的影响是在客户端,比如Web API或任何DB消费者上,而不是在DB服务器上)
...
hostssl all all 127.0.0.1/32 cert clientcert=1
...
但一个简单的Node项目也可以连接无需SSL:
require('dotenv').config({ path: './environment/PostgreSql.env'});
const pgp = require('pg-promise')();
const db = pgp(
{
user: process.env.PGuser,
host: process.env.PGhost,
database: process.env.PGdatabase,
password: process.env.PGpassword,
port: process.env.PGport,
ssl: false // optional, but true gets code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
}
);
var sql = 'select * from current.testssl()';
db.any(sql)
.then
(
good =>
{
console.log(good); // ssl false gets data
},
bad =>
{
console.log(bad);
/* ssl true gets
at TLSWrap.callbackTrampoline (internal/async_hooks.js:130:17)
{
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
stack: 'Error: unable to verify the first certificate…ckTrampoline (internal/async_hooks.js:130:17)',
message: 'unable to verify the first certificate'
}
*/
}
);
最终解决方案基于@Lauranz Albe和@jjanes的pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
hostnossl all all 0.0.0.0/0 reject # must be the 1st line!
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
hostssl all all 0.0.0.0/0 cert clientcert=verify-full
.env
包含了上面代码中显示的内容:user, host, databasename, password
等等。请完成上面 pg_hba.conf 的内容。 - Jeb50http://127.0.0.1:53819/browser/
上连接而不需要httpS。谢谢! - Jeb50