与 node.js 的 postgres 连接中的 SASL 错误

SASL error in postgres connection with node.js

我在尝试连接到我的数据库时遇到以下错误:

Conexão estbabelecida com sucesso
Acessar http://localhost:3000
Servidor executando na porta 3000
C:\Users\lopes\Desktop\Javascript\novo-projeto\ExpressEWebpack\node_modules\pg\lib\sasl.js:24
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string')
    ^

Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
    at Object.continueSession (C:\Users\lopes\Desktop\Javascript\novo-projeto\ExpressEWebpack\node_modules\pg\lib\sasl.js:24:11)
    at Client._handleAuthSASLContinue (C:\Users\lopes\Desktop\Javascript\novo-projeto\ExpressEWebpack\node_modules\pg\lib\client.js:257:10)
    at Connection.emit (events.js:315:20)
    at C:\Users\lopes\Desktop\Javascript\novo-projeto\ExpressEWebpack\node_modules\pg\lib\connection.js:114:12
    at Parser.parse (C:\Users\lopes\Desktop\Javascript\novo-projeto\ExpressEWebpack\node_modules\pg-protocol\dist\parser.js:40:17)
    at Socket.<anonymous> (C:\Users\lopes\Desktop\Javascript\novo-projeto\ExpressEWebpack\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
[nodemon] app crashed - waiting for file changes before starting...

这是我的连接代码

require('dotenv').config();
const express = require('express');
const app = express(); 
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(process.env.DB_URL);

try {
    sequelize.authenticate();
    console.log('Conexão estbabelecida com sucesso')
}catch{
    console.log('Erro ao estabelecer conexão');
}

const session = require('express-session');
const flash = require('connect-flash');

const routes = require('./routes')
const path = require('path');


app.use(express.urlencoded({  extended:true }));

app.use(express.static(path.resolve(__dirname, 'public')));

const sessionOptions = session({
    secret:process.env.cookie_secret,
    store: new (require('connect-pg-simple')(session))(),
    resave: false,
    saveUninitialized: false,
    cookie: {maxAge: 1000 * 60 * 60 * 24 * 7}
});

app.use(sessionOptions);
app.use(flash());

app.set('views', path.resolve(__dirname, 'src', 'views'));
app.set('view engine', 'ejs');

app.use(routes);

app.listen(3000, () => {
    console.log('Acessar http://localhost:3000');
    console.log('Servidor executando na porta 3000');
});

我有这个错误,但我的数据库根本没有密码,我没有密码这一事实会干扰连接吗?在某种程度上,密码因为没有价值而收到空值?我试图解决,查看 sasl.js 上的内容,但没有多大帮助

在您的 pg_hba.conf 文件中(在 Windows 上,目录是 \Program Files\PostgreSQL\data ) 将 IPv4IPv6 本地连接的方法更改为 trustscram-sha-256 加密机制需要密码,这就是它失败的原因。