与指定的 sslrootcert 连接的 Pg-Promise 问题
Pg-Promise problem connecting with sslrootcert specified
我在尝试连接到具有 sslmode=require 的 Digital Ocean 上的托管数据库时遇到问题。
我得到的连接字符串如下:
postgresql://username:password@server:port/mydb?sslmode=require
我的 db.js 文件如下所示:
"use strict";
const fs = require('fs')
const pgPromise = require("pg-promise");
const {ConnectionString} = require('connection-string');
const path = require('path');
var options = {
// Initialization Options
};
let pgp = pgPromise(options);
const dotenv = require('dotenv');
dotenv.config();
const a = new ConnectionString('postgresql://username:password@server:port/mydb?sslmode=require')
var cert= fs.readFileSync(__dirname + '/certs/ca-certificate.crt', 'utf8')
a.setDefaults({
params: {
sslrootcert : cert
}
});
var connstring = a.toString();
let dbpool = pgp(connstring);
module.exports = { dbpool };
当我最初启动这个过程时,一切似乎都很顺利,但是当我尝试访问数据库时,我得到:
Error: ENOENT: no such file or directory, open 'C:\Users\Me\Documents\GitHub\tester\-----BEGIN CERTIFICATE----- certificate info -----END CERTIFICATE-----
如果我更改 pgp 连接以接受 ConnectionString 对象
例如。
let dbpool = pgp(a);
然后我似乎连接到服务器,但出现身份验证错误。将我的连接字符串更改为指向我的本地数据库 let dbpool = pgp(a)
导致我收到奇怪的错误,例如列不存在。但是用 let dbpool = pgp(connstring);
指向本地似乎工作正常。因此,我假设我需要使用 let dbpool = pgp(connstring);
.
我的其余相关代码(这只是一个连接到托管数据库的简单测试项目)如下:
routes/index.js
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const userrepository_1 = require("../repositories/userrepository");
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
/* HIT DATABASE. */
router.get('/testdb', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
let userRepos = new userrepository_1.UserRepository();
let userid = yield userRepos.getuserbyusername("myusername");
if (userid == null) {
return res.status(404).send({ auth: false, message: 'No user found' });
}
res.render('dbtest', { userid: userid });
}))
module.exports = router;
repositories/userrepository.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dbProvider = require("../db");
class UserRepository {
constructor() {
this.db = dbProvider.dbpool;
}
getuserbyusername(username) {
return new Promise((resolve, reject) => { resolve(this.db.oneOrNone('SELECT * FROM users
where isdeleted=false AND username=', [username])); })
.then((user) => {
if (user != null) {
let uid = user.userid;
return uid;
}
else {
return null;
}
});
}
}
exports.UserRepository = UserRepository;
我的目录结构是:
/bin
www
/certs
ca-certificate.crt
/node_modules
/public
/repositories
userrepository.js
/routes
index.js
/views
app.js
db.js
就像我说的,我认为问题出在 let dbpool = pgp(connstring);
好的,这是一个简单的修复。无需使用 const cert = fs.readFileSync(__dirname + '/certs/ca-certificate.crt', 'utf8')
读取文件,我只需要指定位置即可。因此:
const path = require('path');
const cs = new ConnectionString('postgresql://username:password@server:port/mydb?sslmode=require');
const sslrootcert = path.join(__dirname, 'ca-certificate.crt');
cs.setDefaults({
params: { sslrootcert }
});
const db = pgp(cs.toString());
(我也将证书移动到我的主目录)
我在尝试连接到具有 sslmode=require 的 Digital Ocean 上的托管数据库时遇到问题。
我得到的连接字符串如下:
postgresql://username:password@server:port/mydb?sslmode=require
我的 db.js 文件如下所示:
"use strict";
const fs = require('fs')
const pgPromise = require("pg-promise");
const {ConnectionString} = require('connection-string');
const path = require('path');
var options = {
// Initialization Options
};
let pgp = pgPromise(options);
const dotenv = require('dotenv');
dotenv.config();
const a = new ConnectionString('postgresql://username:password@server:port/mydb?sslmode=require')
var cert= fs.readFileSync(__dirname + '/certs/ca-certificate.crt', 'utf8')
a.setDefaults({
params: {
sslrootcert : cert
}
});
var connstring = a.toString();
let dbpool = pgp(connstring);
module.exports = { dbpool };
当我最初启动这个过程时,一切似乎都很顺利,但是当我尝试访问数据库时,我得到:
Error: ENOENT: no such file or directory, open 'C:\Users\Me\Documents\GitHub\tester\-----BEGIN CERTIFICATE----- certificate info -----END CERTIFICATE-----
如果我更改 pgp 连接以接受 ConnectionString 对象
例如。
let dbpool = pgp(a);
然后我似乎连接到服务器,但出现身份验证错误。将我的连接字符串更改为指向我的本地数据库 let dbpool = pgp(a)
导致我收到奇怪的错误,例如列不存在。但是用 let dbpool = pgp(connstring);
指向本地似乎工作正常。因此,我假设我需要使用 let dbpool = pgp(connstring);
.
我的其余相关代码(这只是一个连接到托管数据库的简单测试项目)如下:
routes/index.js
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const userrepository_1 = require("../repositories/userrepository");
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
/* HIT DATABASE. */
router.get('/testdb', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
let userRepos = new userrepository_1.UserRepository();
let userid = yield userRepos.getuserbyusername("myusername");
if (userid == null) {
return res.status(404).send({ auth: false, message: 'No user found' });
}
res.render('dbtest', { userid: userid });
}))
module.exports = router;
repositories/userrepository.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dbProvider = require("../db");
class UserRepository {
constructor() {
this.db = dbProvider.dbpool;
}
getuserbyusername(username) {
return new Promise((resolve, reject) => { resolve(this.db.oneOrNone('SELECT * FROM users
where isdeleted=false AND username=', [username])); })
.then((user) => {
if (user != null) {
let uid = user.userid;
return uid;
}
else {
return null;
}
});
}
}
exports.UserRepository = UserRepository;
我的目录结构是:
/bin
www
/certs
ca-certificate.crt
/node_modules
/public
/repositories
userrepository.js
/routes
index.js
/views
app.js
db.js
就像我说的,我认为问题出在 let dbpool = pgp(connstring);
好的,这是一个简单的修复。无需使用 const cert = fs.readFileSync(__dirname + '/certs/ca-certificate.crt', 'utf8')
读取文件,我只需要指定位置即可。因此:
const path = require('path');
const cs = new ConnectionString('postgresql://username:password@server:port/mydb?sslmode=require');
const sslrootcert = path.join(__dirname, 'ca-certificate.crt');
cs.setDefaults({
params: { sslrootcert }
});
const db = pgp(cs.toString());
(我也将证书移动到我的主目录)