Deno中Sequelize连接PostgreSQL失败:tlssock._start报错求助
Deno环境下Sequelize连接PostgreSQL的SSL兼容性问题解决
问题分析
你遇到的SequelizeConnectionError: tlssock._start is not a function错误,本质是Deno的TLS实现与Node.js存在差异,npm版pg库在Deno环境中处理SSL连接时出现兼容性问题——Node.js的TLS API与Deno的API不兼容,导致pg库的SSL连接逻辑无法正常执行。
解决方案
方案1:显式指定pg模块并调整SSL配置
修改你的db.ts代码,在Sequelize配置中显式传入导入的pg模块,并调整SSL配置以适配Deno环境:
import { Sequelize } from "npm:sequelize@6.32.1"; // deno-types="npm:@types/pg@8.10.2" import pg from "npm:pg@8.11.1"; import hstore from "npm:pg-hstore@2.3.4"; const sequelize = new Sequelize( "postgres://default:****@example.com:5432/test", { dialectModule: pg, // 强制Sequelize使用你导入的pg模块 dialectOptions: { ssl: true, // 简化SSL配置,让pg自动适配Deno的TLS环境 }, } ); sequelize .authenticate() .then(() => { console.log("Connection has been established successfully."); }) .catch((err) => { console.error("Unable to connect to the database", err); });
如果简化SSL配置后仍有问题,尝试使用Deno兼容的SSL选项格式:
dialectOptions: { ssl: { enabled: true, rejectUnauthorized: false, // 保持原有安全配置 }, }
方案2:确保Deno运行权限
运行代码时,必须授予Deno网络访问权限(如果需要读取本地证书文件,还要加上文件读取权限):
deno run --allow-net --allow-read db.ts
验证
调整配置并赋予权限后重新运行代码,应该能像Node.js环境一样成功输出Connection has been established successfully.。
内容的提问来源于stack exchange,提问作者Reza Attar
相关产品推荐
相关产品推荐

