【发布时间】:2021-11-13 11:49:57
【问题描述】:
我目前正在尝试从数据库创建一个功能查询以将其发布到创建的 csv 文件中,但是我无法以编程方式连接到 PSQL 主机。所以我想做的是:-
- 连接数据库并查询结果
- 将结果推送到 Excel 文件
- 继续()
- SFTP 服务器上的 SFTP 结果并将文件放在目录中。
我可以通过 CLI 中的以下命令手动连接到 PostgresDB:-
- ssh username@xx.xx.xx.xx //不需要密码,因为我的 id_rsa 密钥存储在服务器上
- psql -U username -h LOCALHOST -p 5432 -d databasename pass-password(手动输入)
此外,通过 Visual Studio Code 连接也可以,但是我需要连接到服务器(远程连接),然后使用 postgres 驱动程序连接到数据库。
经过调查,我认为我首先需要使用 SSH 连接到服务器,然后才允许我访问数据库。
这就是我通过代码处理它的方式:-
索引.js
const serverConnectionParams = require('./src/config/serverConn');
function testConnectionServer() {
try {
serverConnectionParams.connectToServer();
} catch (err) {
console.error(err);
}
}
testConnectionServer();
serverConn.js
const { Client } = require('ssh2');
const { readFileSync } = require('fs');
const databaseConnectionParams = require('./databaseConn');
function connectToServer() {
const conn = new Client();
conn.on('ready', () => {
console.log('Client :: ready');
conn.exec('uptime', (err, stream) => {
if (err) throw err;
databaseConnectionParams.auth(); *// This is the database connection param*
stream.on('data', (data) => {
console.log('STDOUT: ' + data);
}).stderr.on('data', (data) => {
console.log('STDERR: ' + data);
});
});
}).connect({
host: 'xx.xx.xx.xx',
username: 'username',
privateKey: readFileSync('src/key/id_rsa')
});
}
exports.connectToServer = connectToServer;
databaseConn.js
const { readFileSync } = require('fs');
const envParam = require('./env.js');
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(envParam.database, envParam.username, envParam.password, {
host: envParam.host,
dialect: envParam.dialect,
ssl: true,
pool: {
max: envParam.pool.max,
min: envParam.pool.min,
acquire: envParam.pool.aquire,
idle: envParam.pool.idle
}
});
async function auth() {
try {
console.log('trying to connect')
sequelize.validate();
} catch (error) {
console.error('Unable to connect to the database:', error);
}
}
exports.auth = auth;
env.js
const env = {
database: 'databasename',
username: 'username',
password: 'password',
host: 'ip@',
dialect: 'postgres',
pool: {
max: 5,
min: 0,
aquire: 30000,
idle: 10000
}
};
module.exports = env;
运行我的节点 index.js 后,我收到以下错误声明:-
Client :: ready
trying to connect
STDOUT: 10:43:09 up 1:21, 1 user, load average: 5.71, 6.03, 5.15
C:\Users\~\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:184
reject(new sequelizeErrors.ConnectionError(err));
^
ConnectionError [SequelizeConnectionError]: no pg_hba.conf entry for host "xx.xx.xx.xx", user "username", database "databasename", SSL off
at Client._connectionCallback
{
parent: error: no pg_hba.conf entry for host "xx.xx.xx.xx", user "username", database "databasename", SSL off
at Parser.parseErrorMessage
{
length: 154,
severity: 'FATAL',
code: '28000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '490',
routine: 'ClientAuthentication'
},
original: error: no pg_hba.conf entry for host "x.x.x.x", user "username", database "password", SSL off
at Parser.parseErrorMessage (C:\Users\~\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\~\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\~\node_modules\pg-protocol\dist\parser.js:39:38)
at Socket.<anonymous> (C:\Users\~\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
length: 154,
severity: 'FATAL',
code: '28000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '490',
routine: 'ClientAuthentication'
}
}
调查错误代码:28000
找到此链接,将问题解释为身份验证尝试失败
https://help.heroku.com/DR0TTWWD/seeing-fatal-no-pg_hba-conf-entry-errors-in-postgres
在网上也找到了几个解决方案,关于pg_hba.conf需要使用md5然后重启postgress(没试过,因为我无法重启postgress服务)
error: Ident authentication failed for user
找到另一个解决方案,解释它是 SSL 问题(试过了,但没有用)
Node.js, PostgreSQL error: no pg_hba.conf entry for host
使用 SSL 后,它会将错误代码更改为以下内容:-
SequelizeConnectionError: self signed certificate
在这里找到了解决方案:-
SequelizeConnectionError: self signed certificate
在我提出之后,它会给我一个不同的错误,rejectUnauthorized 已贬值且版本非常旧(目前似乎无法重现错误代码)
所以我的手现在被束缚了,任何帮助都会很棒!
我也尝试过使用不同的 Javascript 模块而不是 sequelize,但是它们都有相同的身份验证问题。
我也尝试传递我的 id_rsa 密钥,但它根本无法解决我的问题。
我的假设是,即使我在 SSH 连接中传递 connToDatabase 函数,它仍在错误位置搜索 ip@。 (服务器上DB的Ip@是192.168.31.4)
但在使用该 IP@ 时,它会显示 ERR 连接超时
我的另一个假设是数据库有很多连接限制,需要更多参数。
更新:
我尝试通过 VSC 上的远程访问来编辑 pg_hba.conf 文件,但它会给我错误无法读取文件。
任何帮助都会很棒!
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: node.js database postgresql database-connection devops