我目前试图创建一个功能查询从数据库post它创建一个csv文件,但我无法以编程方式连接到PSQL主机。所以我要做的是
连接DB并查询结果
将结果推送到Excel文件
继续()
SFTP结果到自己的SFTP服务器上,并将文件放在目录下。
在命令行模式下,可以通过以下方式手动连接到PostgresDB
ssh username@xx。xx。xx。xx //不需要密码,因为我的id_rsa密钥存储在服务器上
psql - u username -h LOCALHOST -p 5432 -d databasename pass- password(手动输入)
此外,通过Visual Studio代码连接也可以,但我需要连接到服务器(远程连接),然后使用postgres驱动连接到数据库。
在调查之后,我认为我首先需要使用SSH连接到服务器,然后,只有这样我才会被允许访问数据库。
这是我如何通过Code来实现它:-
Index。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;
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;
在运行我的node 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服务)
错误:用户身份验证失败
找到了另一个解决方案,解释这是一个SSL问题(尝试它,它没有工作)
Node。js PostgreSQL错误:主机没有pg_hba。conf条目
使用SSL后,它将更改错误代码为以下:-
SequelizeConnectionError: self signed certificate
我在这里找到了一个解决办法:-
SequelizeConnectionError:自签名证书
在我写完之后,它会给我一个不同的错误,拒绝tunauthorized是折旧和非常旧的版本(似乎无法复制错误代码的时刻)
所以我的手被绑在此刻任何帮助将是伟大的!
我也试过使用不同的Javascript模块,而不是sequelize,但他们都有相同的认证问题。
我也试图通过我的id_rsa密钥,但它不会解决我的问题。
我的假设是,即使我在SSH连接中传递了connToDatabase函数,它仍然在不正确的位置中搜索ip@。(服务器上DB的Ip@为192。168。31。4)
但是当使用IP@时,它会说ERR Connection Timed out
我的另一个假设是,数据库在连接方面有很多限制,需要更多的参数。
更新:
我尝试编辑pg_hba。conf文件通过远程访问VSC,但它会给我错误无法读取文件。
任何帮助都太好了!
###我完全忘记我发布了这个问题。
解决方案是相当直接的,经过一段时间的研究,我意识到我在连接参数上犯了一个错误。
对于其他需要帮助的人,我将以一种简单的方式张贴解决方案。
基本上,我首先需要SSH到服务器,并在连接中添加一条隧道来连接数据库。然后,只有这样,我的sequelize参数通过数据库,因为我已经完全连接到服务器和内部postgresql数据库。
所以TLDR
SSH→隧道→Sequelize
ssh (10。 x。x。1等)→addTunnel(本地主机等)→sequelize(数据库名等)